Assembly code compiles but does not display output in asmloader

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>%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
</code>
<code>%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 </code>
%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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>%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
</code>
<code>%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 </code>
%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:

  1. Initial Code:
    This code is supposed to perform long addition of two numbers a and b, handling carries, and displaying the result. It compiles successfully but does not produce any output in asmloader.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>%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
    </code>
    <code>%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 </code>
    %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
    
  2. Simpler Code:
    This simpler version works and displays the result but does not handle carry properly for long addition.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>%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
    </code>
    <code>%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 </code>
    %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!

New contributor

Dawid Olko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật