Difference between revisions of "CSC231 Homework 4 Solutions 2014"
(Created page with "--~~~~ ---- <br /> __TOC__ <br /> =Problem #1= <br /> <source lang="asm"> <br /> ;;; ; ------------------------------------------------------------------- ;;; ; hw4a.asm ;;; ;...") |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
__TOC__ | __TOC__ | ||
<br /> | <br /> | ||
+ | <onlydft> | ||
=Problem #1= | =Problem #1= | ||
+ | <br /> | ||
<br /> | <br /> | ||
<source lang="asm"> | <source lang="asm"> | ||
− | + | ||
;;; ; ------------------------------------------------------------------- | ;;; ; ------------------------------------------------------------------- | ||
;;; ; hw4a.asm | ;;; ; hw4a.asm | ||
Line 71: | Line 73: | ||
_start: mov ecx, N | _start: mov ecx, N | ||
− | for: | + | for: push ecx ;Main loop, loop for N=10 times |
mov esi, msg | mov esi, msg | ||
add esi, dword[counter] ;esi points to integer | add esi, dword[counter] ;esi points to integer | ||
Line 83: | Line 85: | ||
int 0x80 ;print first letter in cap | int 0x80 ;print first letter in cap | ||
− | + | mov ecx, 0 | |
+ | mov ecx, byte[esi] ;move the integer to ecx | ||
dec ecx ;minus the already printed first letter | dec ecx ;minus the already printed first letter | ||
inc dword[counter] | inc dword[counter] | ||
Line 90: | Line 93: | ||
call printLine | call printLine | ||
− | + | pop ecx | |
loop for | loop for | ||
Line 104: | Line 107: | ||
printLower: | printLower: | ||
forLower: | forLower: | ||
− | + | push ecx | |
+ | |||
mov eax, WRITE | mov eax, WRITE | ||
mov ebx, STDOUT | mov ebx, STDOUT | ||
Line 115: | Line 119: | ||
inc dword[counter] | inc dword[counter] | ||
− | + | pop ecx | |
loop forLower | loop forLower | ||
ret | ret | ||
Line 130: | Line 134: | ||
mov ebx, STDOUT | mov ebx, STDOUT | ||
mov edx, line_len | mov edx, line_len | ||
− | + | push ecx | |
− | + | mov ecx, newLine | |
int 0x80 | int 0x80 | ||
− | + | pop ecx | |
ret | ret | ||
Line 139: | Line 143: | ||
</source> | </source> | ||
+ | <br /> | ||
+ | =Problem 3= | ||
+ | <br /> | ||
+ | <source lang="asm"> | ||
+ | |||
+ | ;;; ; Hw4_3.asm | ||
+ | ;;; ; D. Thiebaut | ||
+ | ;;; ; | ||
+ | ;;; ; displays the first Fibonacci terms along with a string | ||
+ | ;;; ; identifying the terms. | ||
+ | ;;; ; | ||
+ | ;;; ; Example: | ||
+ | ;;; ; Fibonacci(0) =1 | ||
+ | ;;; ; Fibonacci(1) =1 | ||
+ | ;;; ; Fibonacci(2) =2 | ||
+ | ;;; ; Fibonacci(3) =3 | ||
+ | ;;; ; Fibonacci(4) =5 | ||
+ | ;;; ; Fibonacci(5) =8 | ||
+ | ;;; ; ... | ||
+ | ;;; ; To assemble and run: | ||
+ | ;;; ; | ||
+ | ;;; ; nasm -f elf Hw3_5.asm | ||
+ | ;;; ; nasm -f elf 231Lib.asm | ||
+ | ;;; ; ld -melf_i3896 -o Hw3_5 Hw3_5.o 231Lib.o | ||
+ | ;;; ; ./Hw3_5 | ||
+ | ;;; ; ------------------------------------------------------------------- | ||
+ | |||
+ | extern _printDec | ||
+ | |||
+ | ;;; ------------------------------------------------------------ | ||
+ | ;;; data areas | ||
+ | ;;; ------------------------------------------------------------ | ||
+ | |||
+ | section .data | ||
+ | Fibo db "Fibonacci(" | ||
+ | FIBOLEN equ $-Fibo | ||
+ | Equal db ") =" | ||
+ | EQUALLEN equ $-Equal | ||
+ | counter dd 2 | ||
+ | temp dd 0 ; holds ecx as loop counter | ||
+ | |||
+ | ;;; ------------------------------------------------------------ | ||
+ | ;;; code area | ||
+ | ;;; ------------------------------------------------------------ | ||
+ | |||
+ | section .text | ||
+ | global _start | ||
+ | ;;; -------------------------------------------------- | ||
+ | ;;; printLn: prints a new line | ||
+ | ;;; -------------------------------------------------- | ||
+ | printLn: | ||
+ | section .data | ||
+ | msg1 db 0x0a | ||
+ | section .text | ||
+ | push eax | ||
+ | push ebx | ||
+ | push ecx | ||
+ | push edx | ||
+ | |||
+ | mov eax, 4 | ||
+ | mov ebx, 1 | ||
+ | mov ecx, msg1 | ||
+ | mov edx, 1 | ||
+ | int 0x80 | ||
+ | |||
+ | pop edx | ||
+ | pop ecx | ||
+ | pop ebx | ||
+ | pop eax | ||
+ | ret | ||
+ | |||
+ | ;;; -------------------------------------------------- | ||
+ | ;;; printMsg: prints the message passed in ecx (address) | ||
+ | ;;; and edx (# of chars). Does not modify eax or ebx. | ||
+ | ;;; -------------------------------------------------- | ||
+ | printMsg: push eax | ||
+ | push ebx | ||
+ | mov eax, 4 | ||
+ | mov ebx, 1 | ||
+ | int 0x80 | ||
+ | pop ebx | ||
+ | pop eax | ||
+ | ret | ||
+ | |||
+ | ;;; -------------------------------------------------- | ||
+ | ;;; printFibMsg: gets a number in eax, and prints it | ||
+ | ;;; between parentheses, as in "Fibonacci(###) = " | ||
+ | ;;; -------------------------------------------------- | ||
+ | printFibMsg: push eax | ||
+ | push ebx | ||
+ | push ecx | ||
+ | push edx | ||
+ | |||
+ | ;; print "Fibonacci(" | ||
+ | mov ecx, Fibo | ||
+ | mov edx, FIBOLEN | ||
+ | call printMsg | ||
+ | ;; print eax | ||
+ | call _printDec | ||
+ | ;; print ")=" | ||
+ | mov ecx, Equal | ||
+ | mov edx, EQUALLEN | ||
+ | call printMsg | ||
+ | pop edx | ||
+ | pop ecx | ||
+ | pop ebx | ||
+ | pop eax | ||
+ | ret | ||
+ | |||
+ | _start: | ||
+ | |||
+ | ;;; display Fibonacci(0) =1 | ||
+ | mov eax, 0 | ||
+ | call printFibMsg | ||
+ | mov eax, 1 | ||
+ | call _printDec | ||
+ | call printLn | ||
+ | |||
+ | ;;; display Fibonacci(1) =1 | ||
+ | mov eax, 1 | ||
+ | call printFibMsg | ||
+ | mov eax, 1 | ||
+ | call _printDec | ||
+ | call printLn | ||
+ | |||
+ | ;;; start for loop | ||
+ | mov esi, 1 ; esi is Fib[i-1] | ||
+ | mov edi, 1 ; edi is Fib[i-2] | ||
+ | mov ecx, 40-2 | ||
+ | for: | ||
+ | push ecx | ||
+ | mov eax, dword[counter] | ||
+ | inc dword[counter] | ||
+ | call printFibMsg | ||
+ | |||
+ | mov eax, esi ; eax <- Fib[i-1] | ||
+ | add eax, edi ; eax <- FIb[i-1]+Fib[i-2] | ||
+ | |||
+ | call _printDec ; print Fib[i] | ||
+ | call printLn | ||
+ | mov edi, esi | ||
+ | mov esi, eax | ||
+ | pop ecx | ||
+ | loop for | ||
+ | ;;; exit | ||
+ | mov eax,1 | ||
+ | mov ebx,0 | ||
+ | int 0x80 ; final system call | ||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | =Problem #4= | ||
+ | <br /> | ||
+ | <source lang="asm"> | ||
+ | ;;; ; Hw4_4.asm | ||
+ | ;;; ; D. Thiebaut | ||
+ | ;;; ; | ||
+ | ;;; ; This program takes a number in eax and prints it in hex | ||
+ | ;;; ; on the screen. The program provides a global function | ||
+ | ;;; ; called _printHex. | ||
+ | ;;; ; | ||
+ | ;;; ; to assemble and run: | ||
+ | ;;; ; | ||
+ | ;;; ; nasm -f elf -F stabs Hw4_4.asm | ||
+ | ;;; ; ld -melf_i3896 -o Hw4_4 Hw4_4.o | ||
+ | ;;; ; ./Hw4_4 | ||
+ | ;;; ; ------------------------------------------------------------------- | ||
+ | |||
+ | ;;; ------------------------------------------------------------ | ||
+ | ;;; data areas | ||
+ | ;;; ------------------------------------------------------------ | ||
+ | section .data | ||
+ | bin2hex db "0123456789ABCDEF" | ||
+ | ;; table of all the hex characters from 0 to F | ||
+ | |||
+ | ;;; ------------------------------------------------------------ | ||
+ | ;;; code area | ||
+ | ;;; ------------------------------------------------------------ | ||
+ | section .text | ||
+ | global _start | ||
+ | ;;; ------------------------------------------------------------ | ||
+ | ;;; getHexDigit: given 4-bit number in dl, returns the ASCII | ||
+ | ;;; corresponding to its hex digit representation in dl. | ||
+ | ;;; ------------------------------------------------------------ | ||
+ | getHexDigit: | ||
+ | push ebx | ||
+ | |||
+ | mov ebx, bin2hex | ||
+ | and edx, 0xf ;clear all but lower 4 bits | ||
+ | mov dl, byte[ebx+edx] | ||
+ | |||
+ | pop ebx | ||
+ | ret | ||
+ | |||
+ | ;;; ------------------------------------------------------------ | ||
+ | ;;; printString | ||
+ | ;;; ------------------------------------------------------------ | ||
+ | printString: push eax | ||
+ | push ebx | ||
+ | |||
+ | mov eax, 4 | ||
+ | mov ebx, 1 | ||
+ | int 0x80 | ||
+ | |||
+ | pop ebx | ||
+ | pop eax | ||
+ | ret | ||
+ | |||
+ | printLn: | ||
+ | section .data | ||
+ | linefeed db 10 | ||
+ | |||
+ | section .text | ||
+ | push ecx | ||
+ | push edx | ||
+ | |||
+ | mov ecx, linefeed | ||
+ | mov edx, 1 | ||
+ | call printString | ||
+ | |||
+ | pop edx | ||
+ | pop ecx | ||
+ | ret | ||
+ | |||
+ | ;;; ------------------------------------------------------------ | ||
+ | ;;; printHex: | ||
+ | ;;; ------------------------------------------------------------ | ||
+ | section .data | ||
+ | hexString db "00000000" | ||
+ | index dd 7 | ||
+ | |||
+ | section .text | ||
+ | |||
+ | _printHex: push eax | ||
+ | push ebx | ||
+ | push ecx | ||
+ | push edx | ||
+ | |||
+ | mov dword[index],7 | ||
+ | mov ecx, 8 ; 8 digits to create | ||
+ | |||
+ | .for: mov edx, eax ; get copy of dword in edx | ||
+ | and edx, 0xf ; isolate lower 4 bits | ||
+ | call getHexDigit | ||
+ | |||
+ | mov ebx, dword[index] ; ebx points to hexString | ||
+ | mov byte[ebx+hexString], dl | ||
+ | dec dword[index] ; get ready for next digit | ||
+ | |||
+ | shr eax, 4 ; put next digit in least | ||
+ | ; significant position | ||
+ | loop .for | ||
+ | |||
+ | mov ecx, hexString | ||
+ | mov edx, 8 | ||
+ | call printString | ||
+ | |||
+ | pop edx | ||
+ | pop ecx | ||
+ | pop ebx | ||
+ | pop eax | ||
+ | ret | ||
+ | |||
+ | ;;; ------------------------------------------------------------ | ||
+ | ;;; ------------------------------------------------------------ | ||
+ | _start: | ||
+ | |||
+ | mov eax, 0x12345678 | ||
+ | call _printHex | ||
+ | call printLn | ||
+ | |||
+ | mov eax, 0x89ABCDEF | ||
+ | call _printHex | ||
+ | |||
+ | call printLn | ||
+ | |||
+ | |||
+ | ;;; exit() | ||
+ | |||
+ | mov eax,1 | ||
+ | mov ebx,0 | ||
+ | int 0x80 ; final system call | ||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | </onlydft> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | [[Category:CSC231]][[Category:Homework]] |