NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: wezzazzew on January 03, 2016, 12:42:41 AM
-
i have win64 so my professor advised me to use DOSBOX to run .com applications generated through NASM.
i wrote this program that multiply two numbers that the user entered, nut when i try to run it, this happens:
(https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xaf1/v/t34.0-12/12463980_1690447177864457_1795372374_n.jpg?oh=0ba5106988195471ff56c0d32c41ad21&oe=568A62FC&__gda__=1451938989_6754b173c619e486d304a063a8ef2f86)
here's my code
org 100h
section .data
message1 dd 13, "Enter first number", 13, 10, '$'
message2 dd 13, "Enter second", 13, 10, '$'
inputNumber db 3, " "
valueToPrint db 3, " "
numOne db 3, " "
numTwo db 3, " "
section .text
_start:
call multi
mov esi, 0
call convert
jmp safe_exit
multi:
mov dx, message1
mov ah, 9
int 21h
mov dx, inputNumber
mov ah, 0Ah
int 21h
mov esi, 0
mov ecx, inputNumber
top:
mov al, [ecx]
inc ecx
cmp al, 0
jz done
sub al, '0'
jl done
cmp al, 9
jz done
sub al, '9'
jg done
mov ah, 0
push eax
add al, '0'
inc esi
jmp top
done:
call endl
pop edx
dec esi
jz secondStep
pop eax
imul eax, 10
add edx, eax
dec esi
jz secondStep
pop eax
imul eax, 100
add edx, eax
dec esi
jz secondStep
pop eax
imul eax, 1000
add edx, eax
dec esi
jz secondStep
secondStep:
mov [numOne], edx
mov dx, message1
mov ah, 9
int 21h
mov dx, inputNumber
mov ah, 0Ah
int 21h
mov esi, 0
mov ecx, inputNumber
toptwo:
mov al, [ecx]
inc ecx
cmp al, 0
jz done
sub al, '0'
jl done
cmp al, 9
jg done
mov ah, 0
push eax
add al, '0'
inc esi
jmp toptwo
donetwo:
call endl
pop edx
dec esi
jz letsgo
pop eax
imul eax, 10
add edx, eax
dec esi
jz letsgo
pop eax
imul eax, 100
add edx, eax
dec esi
jz letsgo
pop eax
imul eax, 1000
add edx, eax
dec esi
jz letsgo
letsgo:
mov [numTwo], edx
mov eax, [numOne]
mov edx, [numTwo]
mul edx
ret
convert:
mov edx,0
mov ecx,0Ah
idiv ecx
push eax
mov eax,edx
add eax,'0'
push eax
pop ebx
pop eax
push ebx
inc esi
or eax,eax
jnz convert
call endl
labella:
pop eax
call print_char
dec esi
or esi,esi
jnz labella
endl:
mov eax, 10
call print_char
ret
print_char:
mov [valueToPrint], eax
mov dx, valueToPrint
mov ah, 9
int 21h
ret
safe_exit:
mov ah,00h
int 16h
ret
is it something wrong with it that's makig DOSBOX behave this way?
-
First: You should not use 32-bit stuff (eax,ebx,ecx,edx...) in a 16-bit program. It's possible but there are pitfalls.
Second: You should always use a documentation for the system calls (int 21h). Every operating system has different requirements. Look at Ralf Brown's interrupt list for DOS: Int 21/AH=09h (http://www.ctyme.com/intr/rb-2562.htm) and Int 21/AH=0Ah (http://www.ctyme.com/intr/rb-2563.htm) and check if you fulfilled all conditions (register usage, memory defines etc.). Hint: you made mistakes on both calls.
Third: You should use a debugger to let the code run step by step and to see if the program does what it should do. I recommend for 16-bit-DOSBox-MSDOS-programs the Turbo Debugger (google for "turbo debugger download").