hw7_ex2.asm

%include "asm_io.inc"

segment .data
        msg     db      "Enter a desired number of bytes: ",0
        L1      dd      12, 52, 128
        L2      db      "assembly language",0
        L3      dw      0AABBh, 0BBCCh, 0CCDDh, 0DDEEh
        L4      dd      0

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

        mov     eax, msg
        call    print_string
        call    read_int
        push    L1
        push    eax
        call    dumpMem         ; call dumpMem
        add     esp, 8          ; clear the stack

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

;;; This function takes a memory address and a iteration number, prints out a
;;; specified length of memory contents in hex starting from the address.
dumpMem:
        push    ebp             ; setup
        mov     ebp, esp        ; setup
        pusha                   ; setup

        mov     ebx, [ebp + 12] ; let ebx hold the first param, where to start
        mov     ecx, [ebp + 8]  ; let ecx hold the second param, how long to go
dump_loop:
        cmp     ecx, 0          ; terminate when ecx reaches 0
        je      dump_loop_end
        dec     ecx             ; decrement ecx by one each loop
        mov     al, [ebx]       ; move a 1 byte quantity to al
        movzx   eax, al         ; extend al to 4 byte quantity value
        push    eax             ; pass the value as the param of printHex
        call    printHex        ; call printHex
        add     esp, 4          ; clean up the stack
        inc     ebx             ; increment ebx to point to the next byte
        jmp     dump_loop
dump_loop_end:
        call    print_nl        ; print a blank line at the end

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

;;; This function takes a value (0-255), converts it to hex and print it out.
printHex:
        push    ebp             ; setup
        mov     ebp, esp        ; setup
        pusha                   ; setup

        mov     eax, [ebp + 8]  ; let eax hold the only param
        cmp     eax, 256        ; anything larger than 255 is invalid
        jnl     invalid_input
        cmp     eax, 0          ; anything smaller than 0 is invalid
        jl      invalid_input
        mov     edx, 0          ; clean up edx for division operation
        mov     ebx, 16         ; the divisor is 16
        div     ebx
        mov     ecx, edx        ; the first remainder is stored in ecx
        div     ebx             ; the second remainder is stored in edx
        push    edx
        call    printDigit      ; print out the second remainder first
        add     esp, 4
        push    ecx
        call    printDigit      ; then print out the first remainder
        add     esp, 4
        jmp     print_space     ; then print out a white space
invalid_input:
        mov     eax, 63         ; print out ?? for invalid param
        call    print_char
        call    print_char
print_space:                    ; code to print white space
        mov     eax, 32
        call    print_char

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

;;; This function takes a value (0-15), prints out as it is if it's in the
;;; range of 0-9, or prints out a letter in A-F if it's in the range of 10-15.
printDigit:
        push    ebp             ; setup
        mov     ebp, esp        ; setup
        pusha                   ; setup

        mov     eax, [ebp + 8]  ; let eax hold the only param
        cmp     eax, 16         ; anything larger than 15 is invalid
        jnl     invalid_digit
        cmp     eax, 0          ; anything smaller than 0 is invalid
        jl      invalid_digit
        cmp     eax, 10         ; compare the digit with 10
        jl      print_integer   ; print as an integer if it's smaller than 10
        add     eax, 55         ; otherwise print as a character
        call    print_char
        jmp     printDigit_end
print_integer:
        call    print_int
        jmp     printDigit_end
invalid_digit:
        mov     eax, 63         ; print out a "?" for invalid param
        call    print_char

printDigit_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