部件x86_64显示残值

时间:2019-09-19 22:25:05

标签: assembly x86-64

我想从控制台获取一个数字并以1递增的方式显示它。但是结果不是我想要显示的。你能告诉我我做错了什么吗? (我一点也不擅长汇编程序)

.data
number: .long 0
numberformat: .asciz "%ld"
assig: .asciz "text \n \n"
enternumber: .asciz "Enter the number: \n"
numberformat1: .asciz "The input number+1 = %d \n"

.text
.global main
main:           # main
    call print1
    call print2
    call inout
    call exit

inout:
    movq $0, %rax    # clear rax
    movq $numberformat, %rdi    # load format string
    movq $number, %rsi    # set storage to address of number
    call scanf
    pop %rbp
    movq $number, %rbx
    add $1, %rbx
    movq $0, %rax
    movq %rbx, %rsi
    movq $numberformat1, %rdi
    call printf    
    # call printnewnumber
    ret

print1:
    movq $0, %rax
    movq $assig, %rdi
    call printf
    ret

print2:
    movq $0, %rax
    movq $enternumber, %rdi
    call printf
    ret

printnewnumber:
    movq $0, %rax
    movq %rdx, %rsi
    movq $numberformat1, %rdi
    call printf    
    ret

1 个答案:

答案 0 :(得分:1)

movq $number, %rbx行并不复制输入数字本身,而只是复制它的地址。如果您取消引用指针,则将获得所需的输出:

将上述行更改为

movq number(%rip), %rbx   # Dereference pointer and store input number in RBX

收益

Assignment 1b: inout

Enter the number:
123
The input number+1 = 124