Difference between revisions of "CSC231 Homework 4 Solutions 2014"
(7 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
__TOC__ | __TOC__ | ||
<br /> | <br /> | ||
+ | <onlydft> | ||
=Problem #1= | =Problem #1= | ||
<br /> | <br /> | ||
Line 84: | 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 133: | Line 135: | ||
mov edx, line_len | mov edx, line_len | ||
push ecx | push ecx | ||
− | + | mov ecx, newLine | |
int 0x80 | int 0x80 | ||
pop ecx | pop ecx | ||
Line 273: | Line 275: | ||
push ecx | push ecx | ||
mov eax, dword[counter] | mov eax, dword[counter] | ||
+ | inc dword[counter] | ||
call printFibMsg | call printFibMsg | ||
Line 293: | Line 296: | ||
</source> | </source> | ||
<br /> | <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 /> | ||
+ | [[Category:CSC231]][[Category:Homework]] |