%macro mov_dd 2
push eax
push ebx
mov dword eax, [%1]
mov ebx, [eax]
mov dword [%2], ebx
pop ebx
pop eax
%endmacro
section .data
text db "Enter first Number: "
len equ $-text
section .bss
input resb 4
ten_exp_num resb 4
input_ptr resb 4
section .text
global _start
_start:
mov eax, 4
mov ebx, 1
mov ecx, text
mov edx, len
int 0x80 ; print(text)
mov eax, 3
xor ebx, ebx
mov ecx, input
mov edx, 16
int 0x80 ; input = input()
xor edi, edi
xor eax, eax
mov dword [input_ptr], input
push ebp
mov ebp, esp
mov byte [ten_exp_num], 1
call convert
pop ebp
mov [input], eax
mov ecx, input
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
jmp end
convert:
mov_dd input_ptr, input
mov bl, [input]
cmp bl, 10 ; 10 ≙ '\n'
je return_func
call to_int
inc dword [input_ptr]
jmp convert
to_int:
sub bl, "0"
movzx edi, bl
imul edi, [ten_exp_num]
add eax, edi
call ten_expo
ret
ten_expo:
push eax
mov eax, [ten_exp_num]
imul eax, 10
mov [ten_exp_num], eax
pop eax
ret
return_func:
ret
end:
mov eax, 1
xor ebx, ebx
int 0x80 ; return 0
I'm really new to assembly programming and currently trying to program a calculator, to get a basic understanding of it.
When im debugging with gdb and use e.g. 12\n as input, it works all fine until I'm in my convert loop at the third char (after the macro), which should be \n, but actually is just 0x00. I have completely no clue why that happens and already some stuff like changing the
inc dword [input_ptr]
to
inc byte [input_ptr]
but it didnt seem to help.
Can someone tell my why that happens and how to fix it?
(I know my code would convert in wrong order but I don't want to fix it before this works)