【问题标题】:SPARC Assembly Scanf ErrorSPARC 程序集 Scanf 错误
【发布时间】:2016-11-27 22:09:43
【问题描述】:

我已将我的代码附加到这篇文章中。 但是,当我在 gdb 上运行它时,一旦它扫描了第一个数字和第二个数字,它就会给我一个“程序收到信号 SIGSEGV,分段错误”。错误。 我将不胜感激任何帮助来纠正这个问题。 谢谢!

 .align 4
    .section        ".bss"
    input: .skip 4

    .section        ".data"
    format: .asciz "%d"
    string1: .asciz "Enter Number 1:\n"
    string2: .asciz "Enter Number 2:\n"
    string3: .asciz "The sum of %d and %d is %d\n"

    .section        ".text"

    .global main
    main:
    save %sp, -96, %sp

    set string1, %o0
    call printf
    nop
    set format, %o0
    set input, %o1
    call scanf
    nop
    set string2, %o0
    call printf
    nop
    set format, %o0
    set input, %o2
    call scanf
    nop
    add %o1, %o2, %o3
    set string3, %o0
    ld [%o1], %o1
    ld [%o2], %o2
    ld [%o3], %o3
    call printf
    nop
    ret
    restore

    mov 1, %g1
    ta 0

【问题讨论】:

  • 很好,您尝试使用gdb,但尝试使用它以获得更好的效果;)查看哪个指令出错以及原因。另外,注释您的代码,特别是如果您希望其他人提供帮助。 add %o1, %o2, %o3 毫无意义(添加两个指针)。此外,您似乎依赖于被保留的 %o 寄存器,但这些寄存器是被调用者保存的。此外,scanf 的第二次调用设置了%o2 而不是%o1
  • @Jester 谢谢!感谢您的帮助,我能够找出答案。

标签: assembly computer-science cpu-architecture segmentation-fault sparc


【解决方案1】:

我认为它应该看起来更像这样,但我从未编写过 SPARC 程序集 O:)

.align 4
.section        ".bss"
input1: .skip 4
input2: .skip 4

.section        ".data"
format: .asciz "%d"
string1: .asciz "Enter Number 1:\n"
string2: .asciz "Enter Number 2:\n"
string3: .asciz "The sum of %d and %d is %d\n"

.section        ".text"

.global main
main:
save %sp, -96, %sp

set string1, %o0
call printf

set format, %o0
set input1, %o1
call scanf

set string2, %o0
call printf

set format, %o0
set input2, %o1
call scanf

set input1, %o1
ld [%o1], %o1
set input2, %o2
ld [%o2], %o2
add %o1, %o2, %o3

set string3, %o0
call printf
nop
ret
restore

mov 1, %g1
ta 0

【讨论】:

  • 谢谢!感谢您的帮助,我能够找出答案。
【解决方案2】:

感谢 Stefan 和 Jester 的帮助,我能够找出问题所在!

! SungJae Kim

! b321024 !作业 5 ! 2016 年 12 月 2 日

.align 4
.section    ".bss"
input1: .skip 4
input2: .skip 4

.section    ".data"
format: .asciz "%d"
string1: .asciz "Enter Number 1:\n"
string2: .asciz "Enter Number 2:\n"
string3: .asciz "The sum of %d and %d is %d\n"

.section    ".text"

.global main
main:
save %sp, -96, %sp

set string1, %o0
call printf
nop
set format, %o0
set input1, %o1
call scanf
nop
set string2, %o0
call printf
nop
set format, %o0
set input2, %o1
call scanf
nop
set input1, %o1
ld [%o1], %o1
set input2, %o2
ld [%o2], %o2
add %o1, %o2, %o3
set string3, %o0
call printf
nop
ret
restore

mov 1, %g1
ta 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多