Reputation: 33
I am newbie in NASM. I want to add two numbers one from stdin and second hardcoded and after print result on the screen. But the result i got is question mark (�). Here is code:
section .bss
buf: resb 1
res: resb 1
section .text
global _start
_read:
mov eax, 3 ; sys_read
mov ebx, 0 ; stdin
mov ecx, buf ; buffer
mov edx, 1 ; read byte count
int 80h
_adding:
add ecx, 10
sub ecx, '0'
mov [res], ecx
_write:
mov eax, 4 ; sys_write
mov ebx, 1 ; stdout
mov ecx, res ; buffer
mov edx, 1 ; write byte count
int 80h
_exit:
mov eax, 1 ; exit
mov ebx, 0 ; exit status
int 80h
I tryed like samples shows, adding like this:
_adding:
sub ecx, '0'
mov eax, '1' ; put 1
sub eax, '0'
add eax, ecx ; adding 1+'user input'
add eax, '0'
mov [res], eax
but with that i get nothing in output. For any help thank you.
Upvotes: 0
Views: 3062
Reputation: 3083
Here is a working example of your code. It works only, when the result is <10 aka when your result is only 1 digit long.
section .bss
buf: resb 1
res: resb 1
section .text
global _start
_start: ; you made the label _start global,
; but you forgot to add the label.
_read:
mov eax, 3 ; sys_read
mov ebx, 0 ; stdin
mov ecx, buf ; buffer (memory address, where read should save 1 byte)
mov edx, 1 ; read byte count
int 80h
_adding:
mov cl, [buf] ; copy 1 byte (the ASCII-char) from address buf into cl
sub cl, '0' ; same as "sub cl, 30h"; changes ASCII number into binary number. (This is optional)
add cl, 1 ; it will not work, when the result is >9!
; Because then you get 2 digits
add cl, '0' ; convert binary number back to ascii-char.
; (This is optional)
mov [res], cl ; you could use buf instead of res, too.
_write:
mov eax, 4 ; sys_write
mov ebx, 1 ; stdout
mov ecx, res ; buffer
mov edx, 1 ; write byte count
int 80h
_exit:
mov eax, 1 ; exit
mov ebx, 0 ; exit status
int 80h
Upvotes: 1
Reputation: 58762
You forgot to even use the value you read. It's in [buf]
of course. Also, it's a byte
not a dword
(although the latter accidentally happens to work in this case). Furthermore, the simple way you are trying only works for single digit numbers, so adding 10 is already doomed. Also, you adjust the input by subtracting '0'
but don't add that back for output (like it's done in your example at the bottom).
Upvotes: 0
Reputation: 20818
It seems you are reading ASCII from the terminal (1 character) then adding 10 to that one character and subtracting '0' (0x30). What character are you entering? If you are entering a digit ('0' to '9' which is 0x30 to 0x39), the result will be the ASCII code of 0x0A to 0x13, which are unprintable control characters.
Try changing the add acx,10
to add acx,1
and remove the line that has sub ecx,'0'
. If you enter '0', you should get '1'.
If that doesn't work, then you have additional bugs in your IO. I don't know enough about int 80h
to help with that.
Upvotes: 0