%include "asm_io.inc"
segment .data
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 ebx, L1 ; ebx = L1
main_loop:
movzx ecx, byte [ebx] ; cl = byte at address ebx
push ecx ; put ecx on the stack
call printHex ; call printHex
add esp, 4 ; pop one element off the stack
inc ebx ; increment ebx
cmp ebx, L4 ; compare ebx and L4
jnz main_loop ; if != then continue
call print_nl ; print a new line
end:
popa ; cleanup
mov eax, 0 ; cleanup
leave ; cleanup
ret ; cleanup
;;; 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