So I started learning assembly 4 days ago (I started with 8086 assembly and moved to masm32 since I want to run my programs on windows) and I am currently trying to set up some handy procedures and macro's so I can make coding bigger programs easier. I started working on a procedure to print integers to the console (252 would be displayed as '252' instead of the ascii character for 2, 5 & 2 again), but for some reason it isn't working as I intended, I'm not getting any errors and the program doesn't even halt. Could someone help me figure out why?
Output (the program only prints the 'é' character and then does nothing until I stop it):
é
main procedure:
main proc
; Getting the console input & output handles and storing them in consoleInHandle & consoleOutHandle
invoke GetStdHandle, STD_INPUT_HANDLE
mov consoleInHandle, eax
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov consoleOutHandle, eax
mPrintUInt32 251
invoke ExitProcess, 0
main endp
mPrintUInt32 macro:
mPrintUInt32 macro integer ; Prints the integer in decimal to the console
pusha
mov edx, 0
mov eax, integer
mov max, 1000000000
call pPrintUInt32
popa
endm
pPrintUInt32 procedure:
pPrintUInt32 proc ; Prints a 32-bit unsigned integer to the console
; Divide EDX:EAX by r/m32 and store the result in EAX and the remainder in EDX
; Result: EAX, remainder: EDX
cmp max, 1 ; If max is equal to 1 we know that we have cycled through all digits and can now stop
je stop
div max ; Divide the input by max
cmp eax, 0 ; If the input is smaller than max (if the result is 0, the whole number is in the remainder)
je next_digit ; Go to the next digit
add eax, '0' ; Turn the result into an ascii character by adding the '0' character
mPrintChar al ; Print the current digit
next_digit:
push edx ; Push the remainder
mov ebx, 10 ; Store 10 in ebx so we can divide max
div max ; Divide max by 10
mov max, eax ; Store the result of the division back into max
pop edx ; Pop the remainder so we can use it as input
call pPrintUInt32 ; Call pPrintUInt32 with the remainder as input
stop:
ret
pPrintUInt32 endp
mPrintChar macro (seems to be working fine, I've tested it outside of the pPrintUInt32 procedure and it behaves as it should):
mPrintChar macro character ; Prints the given character to the console
push bx
mov bl, character
call pPrintChar
pop bx
endm
pPrintChar procedure (also works fine):
pPrintChar proc ; Prints the character stored in bl
mov char, bl ; Store the character in memory
invoke WriteConsole, consoleOutHandle, offset char, sizeof char, offset bytesWritten, 0 ; Print the character to the console
ret
pPrintChar endp
Any help is appreciated!