I'm trying to get idiv
working properly and I've read you put what you want to divide
such as 25 and then in ebx
you put what you want to divide by such as 5 then you do
idiv ebx
which then puts EAX
= 5 and then EDX
= 0.
However it doesn't do that in my program the input I do is 100000000
output:
Kilobytes: 100000000
Megabytes: 1869375819
Was wondering what am I doing wrong here?
%include "asm_io.inc"
;
; initialized data is put in the .data segment
;
segment .data
prompt db "Please enter the number of bytes:", 0
param db "1 = Calculate it in kilobytes", 0Ah, "2 = Calculate it in megabytes", 10, 0
output db "Kilobytes: %d", 0Ah, "MegaBytes: %d", 10, 0
;
;
segment .bss
;
input resd 1
input2 resd 1
choice resd 1
;
; code is put in the .text segment
;
segment .text
global asm_main
extern printf
asm_main:
enter 0,0
pusha
mov eax, prompt
call print_string
call read_int
mov [input], eax
mov [input2], eax
sub esp, 10h
push dword [output]
push dword [input2]
push dword [input]
mov eax, param
call print_string
call read_int
cmp eax, 1
je kilobyte; Jump if eax is equal to 1
cmp eax, 2
je megabyte; Jump if eax equal to 2
kilobyte:
pop eax ; Pop input into eax
cdq
mov ebx, 1024 ; Put 1024 into ebx
idiv ebx ; EAX = 100000000/1024 = 97656
mov [input], eax ; Move 97656 into var=input
push dword [input] ; Put into stack
jmp megabyte
megabyte:
pop eax ; Pop input into eax
cdq
mov ebx, 1048576 ; Put 1048576 into ebx
idiv ebx ; EAX = 100000000/1024 = 95
mov [input2], eax ; Move 95 into var=input
push dword [input] ; Put into stack
jmp printOut
printOut:
mov dword [esp], output
call printf
add esp, 4 * 3
add esp, 10h
popa
mov eax, 0
leave
ret
UPDATE
Well I put in xor edx, edx
but I am still getting the same output as before. I've looked into my ebook and other sites and it says same thing so I am really unsure of what I am doing wrong. As well tried the idiv
route with no luck.