I am working on a project using asmloader, and I have encountered an issue. My program compiles without any errors, but when I run it in asmloader, it does not display the expected results. Here is the code I am using:
%ifdef COMMENT
; Program to perform long addition of numbers a and b
; Displays the result in the format:
; a = 9237
; b = 1267
; 1 11
; 9237
; + 1267
; ------
; 10504
; Requires asmloader
%endif
[bits 32]
; esp -> [ret] ; ret - return address to asmloader
a equ 9237
b equ 1267
mov eax, a ; move value of a to eax
mov ebx, b ; move value of b to ebx
xor ecx, ecx ; clear carry
xor edi, edi ; clear result
mov esi, 1 ; set multiplier to 1 (units)
.next_digit:
mov edx, eax ; copy value of eax to edx
and edx, 0xF ; extract last digit of a (4-bit mask)
mov edi, ebx ; copy value of ebx to edi
and edi, 0xF ; extract last digit of b (4-bit mask)
add edx, edi ; add digits of a and b
add edx, ecx ; add carry
cmp edx, 10
jl .no_carry ; if no carry, go to .no_carry
sub edx, 10
mov ecx, 1 ; set carry to 1
jmp .store_digit
.no_carry:
xor ecx, ecx ; clear carry
.store_digit:
imul edx, esi ; multiply digit by multiplier (units, tens, hundreds...)
add edi, edx ; add digit to result
shr eax, 4 ; shift right by 4 bits (one digit less)
shr ebx, 4 ; shift right by 4 bits (one digit less)
imul esi, esi, 10 ; go to next digit (multiplier * 10)
cmp eax, 0
jne .next_digit ; if a != 0, repeat loop
cmp ebx, 0
jne .next_digit ; if b != 0, repeat loop
; Final result in edi
mov eax, a ; move value of a to eax (for display)
mov ebx, b ; move value of b to ebx (for display)
mov ecx, edi ; move result to ecx
raddr:
; esp -> [ret]
push ecx ; result
push ebx ; b
push eax ; a
push ebx ; b
push eax ; a
; esp -> [a][b][a][b][result][ret]
call getaddr
format:
db "a = %d", 0xA, 0
db "b = %d", 0xA, 0
db " %4d", 0xA, 0
db "+ %4d", 0xA, 0
db "------", 0xA, 0
db " %5d", 0xA, 0
getaddr:
; esp -> [format][a][b][a][b][result][ret]
call [ebx+3*4] ; printf(format, a, b, a, b, result)
add esp, 5*4 ; esp = esp + 20
; esp -> [ret]
push 0 ; esp -> [0][ret]
call [ebx+0*4] ; exit(0)
%ifdef COMMENT
; a = 9237
; b = 1267
; 1 1 1
; 9237
; + 1267
; ------
; 10504
%endif
; asmloader API
;
; ESP points to a valid stack
; Function arguments are pushed onto the stack
; EBX contains a pointer to the API table
;
; call [ebx + FUNCTION_NUMBER*4] ; call an API function
;
; FUNCTION_NUMBER:
;
; 0 - exit
; 1 - putchar
; 2 - getchar
; 3 - printf
; 4 - scanf
;
; The return value of the function is in EAX.
; After calling a function, arguments are removed from the stack.
;
; https://gynvael.coldwind.pl/?id=387
This code compiles successfully but does not display any results when run in asmloader. I expect it to perform long addition and display the formatted output.
I have also tried a simpler version of the code which works but does not handle carry properly for long addition:
%ifdef COMMENT
; Program to perform long addition of numbers a and b
; Displays the result in the format "a = %dnb = %dn %dn+ %dn------n %dn"
; Requires asmloader
%endif
[bits 32]
; esp -> [ret] ; ret - return address to asmloader
a equ 9237
b equ 1267
mov eax, a ; eax = a
mov ecx, b ; ecx = b
add eax, ecx ; eax = eax + ecx (result in eax)
raddr:
; esp -> [ret]
push eax ; result
push ecx ; b
push a ; a
push ecx ; b
push a ; a
; esp -> [a][b][a][b][result][ret]
call getaddr
format:
db "a = %d", 0xA,
db "b = %d", 0xA,
db " %4d", 0xA,
db "+ %4d", 0xA,
db "------", 0xA,
db " %5d", 0xA, 0
getaddr:
; esp -> [format][a][b][a][b][result][ret]
call [ebx+3*4] ; printf(format, a, b, a, b, result)
add esp, 5*4 ; esp = esp + 20
; esp -> [ret]
push 0 ; esp -> [0][ret]
call [ebx+0*4] ; exit(0)
%ifdef COMMENT
; a = 9237
; b = 1267
; 1 11
; 9237
; + 1267
; ------
; 10504
%endif
; asmloader API
;
; ESP points to a valid stack
; Function arguments are pushed onto the stack
; EBX contains a pointer to the API table
;
; call [ebx + FUNCTION_NUMBER*4] ; call an API function
;
; FUNCTION_NUMBER:
;
; 0 - exit
; 1 - putchar
; 2 - getchar
; 3 - printf
; 4 - scanf
;
; The return value of the function is in EAX.
; After calling a function, arguments are removed from the stack.
;
; https://gynvael.coldwind.pl/?id=387
Could you please help me understand why the first code does not display the results even though it compiles successfully? Any suggestions or insights would be greatly appreciated.
Thank you!
What did you try and what were you expecting?
I tried writing an assembly program to perform long addition of two numbers and display the result in a formatted output using asmloader. Here are the steps I followed and the two versions of the code I tried:
-
Initial Code:
This code is supposed to perform long addition of two numbersa
andb
, handling carries, and displaying the result. It compiles successfully but does not produce any output in asmloader.%ifdef COMMENT ; Program to perform long addition of numbers a and b ; Displays the result in the format: ; a = 9237 ; b = 1267 ; 1 11 ; 9237 ; + 1267 ; ------ ; 10504 ; Requires asmloader %endif [bits 32] ; esp -> [ret] ; ret - return address to asmloader a equ 9237 b equ 1267 mov eax, a ; move value of a to eax mov ebx, b ; move value of b to ebx xor ecx, ecx ; clear carry xor edi, edi ; clear result mov esi, 1 ; set multiplier to 1 (units) .next_digit: mov edx, eax ; copy value of eax to edx and edx, 0xF ; extract last digit of a (4-bit mask) mov edi, ebx ; copy value of ebx to edi and edi, 0xF ; extract last digit of b (4-bit mask) add edx, edi ; add digits of a and b add edx, ecx ; add carry cmp edx, 10 jl .no_carry ; if no carry, go to .no_carry sub edx, 10 mov ecx, 1 ; set carry to 1 jmp .store_digit .no_carry: xor ecx, ecx ; clear carry .store_digit: imul edx, esi ; multiply digit by multiplier (units, tens, hundreds...) add edi, edx ; add digit to result shr eax, 4 ; shift right by 4 bits (one digit less) shr ebx, 4 ; shift right by 4 bits (one digit less) imul esi, esi, 10 ; go to next digit (multiplier * 10) cmp eax, 0 jne .next_digit ; if a != 0, repeat loop cmp ebx, 0 jne .next_digit ; if b != 0, repeat loop ; Final result in edi mov eax, a ; move value of a to eax (for display) mov ebx, b ; move value of b to ebx (for display) mov ecx, edi ; move result to ecx raddr: ; esp -> [ret] push ecx ; result push ebx ; b push eax ; a push ebx ; b push eax ; a ; esp -> [a][b][a][b][result][ret] call getaddr format: db "a = %d", 0xA, 0 db "b = %d", 0xA, 0 db " %4d", 0xA, 0 db "+ %4d", 0xA, 0 db "------", 0xA, 0 db " %5d", 0xA, 0 getaddr: ; esp -> [format][a][b][a][b][result][ret] call [ebx+3*4] ; printf(format, a, b, a, b, result) add esp, 5*4 ; esp = esp + 20 ; esp -> [ret] push 0 ; esp -> [0][ret] call [ebx+0*4] ; exit(0) %ifdef COMMENT ; a = 9237 ; b = 1267 ; 1 1 1 ; 9237 ; + 1267 ; ------ ; 10504 %endif ; asmloader API ; ; ESP points to a valid stack ; Function arguments are pushed onto the stack ; EBX contains a pointer to the API table ; ; call [ebx + FUNCTION_NUMBER*4] ; call an API function ; ; FUNCTION_NUMBER: ; ; 0 - exit ; 1 - putchar ; 2 - getchar ; 3 - printf ; 4 - scanf ; ; The return value of the function is in EAX. ; After calling a function, arguments are removed from the stack. ; ; https://gynvael.coldwind.pl/?id=387
-
Simpler Code:
This simpler version works and displays the result but does not handle carry properly for long addition.%ifdef COMMENT ; Program to perform long addition of numbers a and b ; Displays the result in the format "a = %dnb = %dn %dn+ %dn------n %dn" ; Requires asmloader %endif [bits 32] ; esp -> [ret] ; ret - return address to asmloader a equ 9237 b equ 1267 mov eax, a ; eax = a mov ecx, b ; ecx = b add eax, ecx ; eax = eax + ecx (result in eax) raddr: ; esp -> [ret] push eax ; result push ecx ; b push a ; a push ecx ; b push a ; a ; esp -> [a][b][a][b][result][ret] call getaddr format: db "a = %d", 0xA, db "b = %d", 0xA, db " %4d", 0xA, db "+ %4d", 0xA, db "------", 0xA, db " %5d", 0xA, 0 getaddr: ; esp -> [format][a][b][a][b][result][ret] call [ebx+3*4] ; printf(format, a, b, a, b, result) add esp, 5*4 ; esp = esp + 20 ; esp -> [ret] push 0 ; esp -> [0][ret] call [ebx+0*4] ; exit(0) %ifdef COMMENT ; a = 9237 ; b = 1267 ; 1 11 ; 9237 ; + 1267 ; ------ ; 10504 %endif ; asmloader API ; ; ESP points to a valid stack ; Function arguments are pushed onto the stack ; EBX contains a pointer to the API table ; ; call [ebx + FUNCTION_NUMBER*4] ; call an API function ; ; FUNCTION_NUMBER: ; ; 0 - exit ; 1 - putchar ; 2 - getchar ; 3 - printf ; 4 - scanf ; ; The return value of the function is in EAX. ; After calling a function, arguments are removed from the stack. ; ; https://gynvael.coldwind.pl/?id=387
What I Expected
I expected the first version of the code to perform long addition with proper handling of carry for each digit and display the result in a formatted output using printf
. The output should show the intermediate steps of addition, including the carried digits.
Problem
Despite the code compiling successfully, there is no output when running it in asmloader. I have confirmed that the printf
function is being called correctly, and the format string is defined properly.
Could anyone help me understand why the first code does not display the results and suggest any fixes?
Thank you!
Dawid Olko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.