Difference between revisions of "CSC231 Homework 4 2015"
(→Preparation) |
|||
Line 11: | Line 11: | ||
=Preparation= | =Preparation= | ||
<br /> | <br /> | ||
+ | * Make sure you have a file in your working directory that contains [[CSC231 Printing & Inputting Decimal Numbers in Assembly| 231Lib.asm]], a library with functions for printing various quantities, as well as for inputting numbers. | ||
* Create 2 different programs in your working directory, one called '''hw4_func.asm''', and the other called '''hw4.asm''', and copy the code below into both of them: | * Create 2 different programs in your working directory, one called '''hw4_func.asm''', and the other called '''hw4.asm''', and copy the code below into both of them: | ||
<br /> | <br /> | ||
Line 109: | Line 110: | ||
eax = 13 | eax = 13 | ||
+ | :Note that the result, 13, is 4 * eax + 1, which is what the function computes, given the number it receives in eax. The result is returned in eax, as well. | ||
<br /> | <br /> | ||
+ | =Your Assignment= | ||
<br /> | <br /> | ||
+ | ::<source lang="asm"> | ||
+ | ;;; ; hw4.asm | ||
+ | ;;; ; D. Thiebaut | ||
+ | ;;; ; | ||
+ | ;;; ; Program for Homework #4 | ||
+ | |||
+ | ;;; ; extern functions that will be linked to this program | ||
+ | ;;; ; contained in 231Lib.asm | ||
+ | |||
+ | extern _printDec | ||
+ | extern _printString | ||
+ | extern _println | ||
+ | extern _getInput | ||
+ | extern computeSum | ||
+ | extern computeAvg | ||
+ | extern swap | ||
+ | extern someFunc | ||
+ | extern teller | ||
+ | |||
+ | ;;; ---------------------------------------------------------- | ||
+ | section .data | ||
+ | a dd 3 | ||
+ | b dd 5 | ||
+ | c dd 7 | ||
+ | d dd 13 | ||
+ | avg dd 0 | ||
+ | sum dd 0 | ||
+ | msg1 db "average = " | ||
+ | MSG1LEN equ $-msg1 | ||
+ | msg2 db "sum = " | ||
+ | MSG2LEN equ $-msg2 | ||
+ | msg3 db "eax = " | ||
+ | msg4 db "ebx = " | ||
+ | msg5 db "3*eax - ebx + 2*ecx - 1 = " | ||
+ | MSG5LEN equ $-msg5 | ||
+ | |||
+ | ;;; ---------------------------------------------------------- | ||
+ | section .text | ||
+ | global _start | ||
+ | _start: | ||
+ | |||
+ | ;; compute sum of a, b, c, and d | ||
+ | mov eax, dword[a] | ||
+ | mov ebx, dword[b] | ||
+ | mov ecx, dword[c] | ||
+ | mov edx, dword[d] | ||
+ | call computeSum | ||
+ | mov dword[sum], eax | ||
+ | |||
+ | mov ecx, msg2 ; print the result | ||
+ | mov edx, MSG2LEN | ||
+ | call _printString | ||
+ | mov eax, dword[sum] | ||
+ | call _printDec | ||
+ | call _println | ||
+ | |||
+ | ;; compute average of a, b, c, and d | ||
+ | mov eax, dword[a] | ||
+ | mov ebx, dword[b] | ||
+ | mov ecx, dword[c] | ||
+ | mov edx, dword[d] | ||
+ | call computeAvg | ||
+ | mov dword[avg], eax | ||
+ | |||
+ | mov ecx, msg1 ; print the result | ||
+ | mov edx, MSG1LEN | ||
+ | call _printString | ||
+ | mov eax, dword[avg] | ||
+ | call _printDec | ||
+ | call _println | ||
+ | |||
+ | ;; swap eax and ebx | ||
+ | |||
+ | mov eax, 37 ; eax <- 37 | ||
+ | mov ebx, 65 ; ebx <- 65 | ||
+ | call swap ; swap the two | ||
+ | |||
+ | mov ecx, msg3 ; display eax | ||
+ | mov edx, 6 | ||
+ | call _printString | ||
+ | call _printDec | ||
+ | call _println | ||
+ | |||
+ | mov ecx, msg4 ; display ebx | ||
+ | mov edx, 6 | ||
+ | call _printString | ||
+ | mov eax, ebx | ||
+ | call _printDec | ||
+ | call _println | ||
+ | |||
+ | ;; compute 3*eax - ebx + 2*ecx - 1 | ||
+ | mov eax, 10 ; initialize | ||
+ | mov ebx, 5 ; eax, ebx, and ecx | ||
+ | mov ecx, 6 | ||
+ | call someFunc ; eax <- 3*eax - ebx + 2*ecx - 1 | ||
+ | |||
+ | mov ecx, msg5 ; display result | ||
+ | mov edx, MSG5LEN | ||
+ | call _printString | ||
+ | call _printDec | ||
+ | call _println | ||
+ | |||
+ | mov eax, 100 ; try new values in eax, ebx, | ||
+ | mov ebx, 5 ; and ecx | ||
+ | mov ecx, 60 | ||
+ | call someFunc | ||
+ | |||
+ | mov ecx, msg5 ; display the result | ||
+ | mov edx, MSG5LEN | ||
+ | call _printString | ||
+ | call _printDec | ||
+ | call _println | ||
+ | |||
+ | ;; break down some amount of $ in $20-, $10-, $5-, and $1-bills | ||
+ | mov eax, 139 ; orginal amount of $ | ||
+ | call _printDec ; display it | ||
+ | call _println | ||
+ | |||
+ | call teller ; break down in 20, 10, 5, and 1 bills | ||
+ | ; passed back in eax, ebx, ecx, and edx. | ||
+ | |||
+ | call _printDec ; print No20s | ||
+ | call _println | ||
+ | |||
+ | mov eax, ebx ; print No10s | ||
+ | call _printDec | ||
+ | call _println | ||
+ | |||
+ | mov eax, ecx ; print No5s | ||
+ | call _printDec | ||
+ | call _println | ||
+ | |||
+ | mov eax, edx ; print No1s | ||
+ | call _printDec | ||
+ | call _println | ||
+ | |||
+ | ;;; exit | ||
+ | mov ebx, 0 | ||
+ | mov eax, 1 | ||
+ | int 0x80 | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | * Given the new contents for '''hw4.asm''', given above, add the missing functions to '''hw4_func.asm''' so that the output of the program will become: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | sum = 28 | ||
+ | average = 7 | ||
+ | eax = 65 | ||
+ | ebx = 37 | ||
+ | 3*eax - ebx + 2*ecx - 1 = 36 | ||
+ | 3*eax - ebx + 2*ecx - 1 = 414 | ||
+ | 139 | ||
+ | 6 | ||
+ | 1 | ||
+ | 1 | ||
+ | 4 | ||
+ | |||
+ | </source> | ||
<br /> | <br /> | ||
+ | =Submission= | ||
<br /> | <br /> | ||
+ | * Submit your '''hw4_func.asm''' file (only, no need for hw4.asm) on Moodle, in the HW 4 PB 1 section. | ||
<br /> | <br /> | ||
<br /> | <br /> |
Revision as of 18:26, 14 October 2015
--D. Thiebaut (talk) 19:11, 14 October 2015 (EDT)
Page under construction!
Preparation
- Make sure you have a file in your working directory that contains 231Lib.asm, a library with functions for printing various quantities, as well as for inputting numbers.
- Create 2 different programs in your working directory, one called hw4_func.asm, and the other called hw4.asm, and copy the code below into both of them:
- hw4.asm:
;;; ; hw4.asm ;;; ; D. Thiebaut ;;; ; ;;; ; Demo program for Homework 4 ;;; ; extern functions that will be linked to this program ;;; ; contained in 231Lib.asm and hw4_func.asm extern _printDec extern _printString extern _println extern _getInput extern demoFunc ;;; ----------------------------------------------------------- section .data a dd 3 msg3 db "eax = " MSG3LEN equ $-msg3 ;;; ----------------------------------------------------------- section .text global _start _start: ;; print "eax = " mov ecx, msg3 mov edx, MSG3LEN call _printString ;; print contents of eax, which contains a mov eax, dword[a] call _printDec call _println ;; pass a to demoFunc through eax mov eax, dword[a] call demoFunc ;; print "eax = " mov ecx, msg3 mov edx, MSG3LEN call _printString ;; print contents of eax, as returned by demoFunc call _printDec call _println ;;; exit mov ebx, 0 mov eax, 1 int 0x80
- hw4_func.asm
;;; ; hw4_func.asm ;;; ; D. Thiebaut ;;; ; Demo program for Homework 4 global demoFunc ;global, so it can be accessed ; from other programs. ;;; ---------------------------------------------------- ;;; demoFunc: returns eax * 4 + 1 ;;; ---------------------------------------------------- demoFunc: add eax, eax add eax, eax inc eax ret
- Assemble, link, and run the programs as follows:
nasm -f elf 231Lib.asm nasm -f elf hw4.asm nasm -f elf hw4_func.asm ld -melf_i386 231Lib.o hw4.o hw4_func.o -o hw4 ./hw4
- Output:
eax = 3 eax = 13
- Note that the result, 13, is 4 * eax + 1, which is what the function computes, given the number it receives in eax. The result is returned in eax, as well.
Your Assignment
;;; ; hw4.asm ;;; ; D. Thiebaut ;;; ; ;;; ; Program for Homework #4 ;;; ; extern functions that will be linked to this program ;;; ; contained in 231Lib.asm extern _printDec extern _printString extern _println extern _getInput extern computeSum extern computeAvg extern swap extern someFunc extern teller ;;; ---------------------------------------------------------- section .data a dd 3 b dd 5 c dd 7 d dd 13 avg dd 0 sum dd 0 msg1 db "average = " MSG1LEN equ $-msg1 msg2 db "sum = " MSG2LEN equ $-msg2 msg3 db "eax = " msg4 db "ebx = " msg5 db "3*eax - ebx + 2*ecx - 1 = " MSG5LEN equ $-msg5 ;;; ---------------------------------------------------------- section .text global _start _start: ;; compute sum of a, b, c, and d mov eax, dword[a] mov ebx, dword[b] mov ecx, dword[c] mov edx, dword[d] call computeSum mov dword[sum], eax mov ecx, msg2 ; print the result mov edx, MSG2LEN call _printString mov eax, dword[sum] call _printDec call _println ;; compute average of a, b, c, and d mov eax, dword[a] mov ebx, dword[b] mov ecx, dword[c] mov edx, dword[d] call computeAvg mov dword[avg], eax mov ecx, msg1 ; print the result mov edx, MSG1LEN call _printString mov eax, dword[avg] call _printDec call _println ;; swap eax and ebx mov eax, 37 ; eax <- 37 mov ebx, 65 ; ebx <- 65 call swap ; swap the two mov ecx, msg3 ; display eax mov edx, 6 call _printString call _printDec call _println mov ecx, msg4 ; display ebx mov edx, 6 call _printString mov eax, ebx call _printDec call _println ;; compute 3*eax - ebx + 2*ecx - 1 mov eax, 10 ; initialize mov ebx, 5 ; eax, ebx, and ecx mov ecx, 6 call someFunc ; eax <- 3*eax - ebx + 2*ecx - 1 mov ecx, msg5 ; display result mov edx, MSG5LEN call _printString call _printDec call _println mov eax, 100 ; try new values in eax, ebx, mov ebx, 5 ; and ecx mov ecx, 60 call someFunc mov ecx, msg5 ; display the result mov edx, MSG5LEN call _printString call _printDec call _println ;; break down some amount of $ in $20-, $10-, $5-, and $1-bills mov eax, 139 ; orginal amount of $ call _printDec ; display it call _println call teller ; break down in 20, 10, 5, and 1 bills ; passed back in eax, ebx, ecx, and edx. call _printDec ; print No20s call _println mov eax, ebx ; print No10s call _printDec call _println mov eax, ecx ; print No5s call _printDec call _println mov eax, edx ; print No1s call _printDec call _println ;;; exit mov ebx, 0 mov eax, 1 int 0x80
- Given the new contents for hw4.asm, given above, add the missing functions to hw4_func.asm so that the output of the program will become:
sum = 28 average = 7 eax = 65 ebx = 37 3*eax - ebx + 2*ecx - 1 = 36 3*eax - ebx + 2*ecx - 1 = 414 139 6 1 1 4
Submission
- Submit your hw4_func.asm file (only, no need for hw4.asm) on Moodle, in the HW 4 PB 1 section.