'Taking user input and incrementing the number by 1
In this code the user is asked to supply a number.
Once they do so the number should be incremented by 1.
This code works when you hard code the value of the number on line 11 (movq $number , %rsi).
If I replace "$Number" with an actual digit like "$5" for example the code will print out the value +1 so in this case '6', but it doesn't seem to work, can someone advise what i am doing wrong? /////////////////////////////////////////////////////////////////// updated code //////////////////////////////////////////////////////////// now works if user input is between 0-9 however errors start if input value > 10
.data
number: .skip 8
.text
mystring: .asciz "Your number +1 is = %d"
.global main
main:
call _getNumber
movq $0 , %rax
movq number, %rsi
sub $2608, %rsi
inc %rsi
movq $mystring, %rdi
call printf
ret
_getNumber: #sub routine to take number from the user
movq $0, %rax #set to 0 for input
movq $0, %rdi
movq $number, %rsi
movq $8, %rdx
syscall
ret
Solution 1:[1]
movq $number, %rsi puts the address of number into %rsi. That's what you want for reading. (Though you will get the bytes of the user input as binary, which is probably not what you want, e.g. if the user types 5 you will probably get the value 53. You have to do the conversion from ASCII decimal to binary by yourself, or by calling an appropriate library function like scanf or strtol. Separate project.)
But for calling printf you don't want the address of number, you want its contents. So in main you ought to have movq number, %rsi, which is AT&T syntax for a load from memory.
In C terms, movq $number, %rsi does rsi = &number;. Whereas movq number, %rsi does rsi = number;.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nate Eldredge |
