Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Draw Stack of NASM assembly code? %include \"asm_io.inc\" segment .data msg1 db

ID: 3833135 • Letter: D

Question

Draw Stack of NASM assembly code?

%include "asm_io.inc"
segment .data
   msg1   db   "Enter n:", 0
   msg2   db   "The sum is:", 0
segment .bss
   value   resd   1

segment .text
global asm_main
asm_main:
   enter   0,0           ; setup
   pusha               ; setup

    mov eax, msg1        ; eax = address of msg1
    call print_string    ; print msg1
    call read_int        ; get an integer from the keyboard (in EAX)
    push eax            ; put the integer on the stack (parameter #1)
    call recursive_sum    ; call recursive_sum
    add esp, 4            ; remove the parameter from the stack
    mov ebx, eax        ; save the value returned by recursive_sum
    mov eax, msg2        ; eax = address of msg2
    call print_string    ; print msg2
    mov eax, ebx        ; eax = sum
    call print_int        ; print the sum
    call print_nl        ; print a new line

   popa               ; cleanup
   mov   eax, 0           ; cleanup
   leave               ; cleanup
   ret               ; cleanup

recursive_sum:
    push ebp            ; save ebp
    mov ebp, esp        ; set EBP = ESP
    pusha                ; save all registers (probably overkill)
    mov ebx, [ebp+8]    ; ebx = integer (parameter #1)
    cmp ebx, 0            ; ebx = 0 ?
    jnz next            ; if (ebx != 0) go to next
    xor ecx, ecx        ; ECX = 0
    jmp end            ; Jump to end
next:
    mov ecx, ebx        ; ECX = EBX
    dec ecx            ; ECX = ECX - 1
    push ecx            ; put ECX on the stack
    call recursive_sum   ; recursive call to recursive_sum!
    add esp, 4            ; pop the parameter from the stack
    add ebx, eax        ; EBX = EBX + recursive_sum(EBX -1)
    mov ecx, ebx        ; ECX = EBX
end:                    ; at this point, ECX contains the result
    mov [value], ecx    ; save ECX, the return value, in memory
    popa                ; restore registers
    mov eax, [value]    ; put the saved returned value into eax
    pop ebp            ; restore EBP
    ret                ; return

Explanation / Answer

%include "asm_io.inc"
segment .data
   msg1   db   "Enter n:", 0
   msg2   db   "The sum is:", 0
segment .bss
   value   resd   1

segment .text
global asm_main
asm_main:
   enter   0,0           ; setup
   pusha               ; setup

    mov eax, msg1        ; eax = address of msg1
    call print_string    ; print msg1
    call read_int        ; get an integer from the keyboard (in EAX)
    push eax            ; put the integer on the stack (parameter #1)
    call recursive_sum    ; call recursive_sum
    add esp, 4            ; remove the parameter from the stack
    mov ebx, eax        ; save the value returned by recursive_sum
    mov eax, msg2        ; eax = address of msg2
    call print_string    ; print msg2
    mov eax, ebx        ; eax = sum
    call print_int        ; print the sum
    call print_nl        ; print a new line

   popa               ; cleanup
   mov   eax, 0           ; cleanup
   leave               ; cleanup
   ret               ; cleanup

recursive_sum:
    push ebp            ; save ebp
    mov ebp, esp        ; set EBP = ESP
    pusha                ; save all registers (probably overkill)
    mov ebx, [ebp+8]    ; ebx = integer (parameter #1)
    cmp ebx, 0            ; ebx = 0 ?
    jnz next            ; if (ebx != 0) go to next
    xor ecx, ecx        ; ECX = 0
    jmp end            ; Jump to end
next:
    mov ecx, ebx        ; ECX = EBX
    dec ecx            ; ECX = ECX - 1
    push ecx            ; put ECX on the stack
    call recursive_sum   ; recursive call to recursive_sum!
    add esp, 4            ; pop the parameter from the stack
    add ebx, eax        ; EBX = EBX + recursive_sum(EBX -1)
    mov ecx, ebx        ; ECX = EBX
end:                    ; at this point, ECX contains the result
    mov [value], ecx    ; save ECX, the return value, in memory
    popa                ; restore registers
    mov eax, [value]    ; put the saved returned value into eax
    pop ebp            ; restore EBP
    ret                ; return

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote