Question

When I give 1 as the input of this code from keyboard, rbx is changed to a31h. But the ASCII value of the number '1' is 30h. So why rbx became a30h?

section .text

global _start

_start:

mov rax,1
mov rdi,1
mov rsi,msg
mov rdx,len
syscall

mov rax,0
mov rdi,0
mov rsi,num
mov rdx,1024
syscall

mov rbx,[num]

mov rax,60
mov rdi,0
syscall

section .data

msg: db 0Ah,"Enter here:",0
len: equ $ - msg

section .bss

num: resb 10
Was it helpful?

Solution

Because you've specified 1024 as the length which means the read syscall is getting two bytes, the 31h representing the character '1', and the 0ah representing the newline.

If you only want one character, only ask for one character, not 1024.

And, if you try to get the full width into rbx, ensure you clear out the upper bits afterwards, something like:

mov rbx, [num]
and rbx, 0ffh

Although a better solution is probably just to load up a byte with zero extension:

movzx rbx, byte [num]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top