hw7_ex3.asm

%include "asm_io.inc"

segment .data
        msg     db      "Values: ",0    ; the output message

segment .text
        global  asm_main
asm_main:
        push    ebp                     ; setup
        mov     ebp, esp                ; setup
        pusha                           ; setup

        push    dword 4                 ; push the first param of f, 4
        push    dword 5                 ; push the second param of f, 5
        call    f                       ; call function f
        add     esp, 8                  ; clean up the stack

        popa                            ; clean up
        mov     eax, 0                  ; clean up
        mov     esp, ebp                ; clean up
        pop     ebp                     ; clean up
        ret                             ; clean up

;;; Function f
f:
        push    ebp                     ; setup
        mov     ebp, esp                ; setup
        pusha                           ; setup

        push    dword 2                 ; push the first param of g, 2
        push    dword [ebp + 12]        ; push the second param of g, x
        push    dword [ebp + 8]         ; push the third param of g, y
        call    g                       ; call function g
        add     esp, 12                 ; clean up the stack

        popa                            ; clean up
        mov     eax, 0                  ; clean up
        mov     esp, ebp                ; clean up
        pop     ebp                     ; clean up
        ret                             ; clean up

;;; Function g
g:
        push    ebp                     ; setup
        mov     ebp, esp                ; setup
        pusha                           ; setup

        cmp     dword [ebp + 16], 0     ; if n == 0, print
        je      print

        mov     eax, [ebp + 12]         ; sum up x and y
        add     eax, [ebp + 8]          ; and store it in eax
        mov     ebx, [ebp + 16]         ; let ebx = n - 1
        dec     ebx
        push    ebx                     ; push n - 1
        push    eax                     ; push x + y
        inc     eax                     ; let eax = x + y + 1
        push    eax                     ; push x + y + 1
        call    g                       ; call function g
        add     esp, 12                 ; clean up stack
        jmp     g_end

print:
        mov     eax, msg                ; print message
        call    print_string
        mov     eax, 91                 ; print [
        call    print_char
        mov     eax, [ebp + 12]         ; print x
        call    print_int
        mov     eax, 44                 ; print ,
        call    print_char
        mov     eax, [ebp + 8]          ; print y
        call    print_int
        mov     eax, 93                 ; print ]
        call    print_char
        call    print_nl                ; print blank line

g_end:
        popa                            ; clean up
        mov     eax, 0                  ; clean up
        mov     esp, ebp                ; clean up
        pop     ebp                     ; clean up
        ret                             ; clean up
Valid HTML 4.01 Valid CSS