Difference between revisions of "CSC231 Homework 4 2015"
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 19:11, 14 October 2015 (EDT) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 19:11, 14 October 2015 (EDT) | ||
---- | ---- | ||
− | < | + | <br /> |
− | < | + | <br /> |
− | </ | + | <bluebox> |
− | < | + | This assignment is due on Wednesday, Oct. 21, at 11:55 p.m. |
− | < | + | </bluebox> |
− | + | <br /> | |
− | + | <showafterdate after="20151014 12:00" before="20151231 00:00"> | |
− | |||
=Preparation= | =Preparation= | ||
<br /> | <br /> | ||
Line 121: | Line 120: | ||
;;; ; extern functions that will be linked to this program | ;;; ; extern functions that will be linked to this program | ||
− | ;;; ; contained in 231Lib.asm | + | ;;; ; contained in 231Lib.asm and in hw4_func.asm |
extern _printDec | extern _printDec | ||
Line 279: | Line 278: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | =Notes= | ||
+ | <br /> | ||
+ | ==Note 1== | ||
+ | <br /> | ||
+ | * You are allowed to declare additional variables in your data section, if you need to. Just add a ''section .data'' block to your '''hw4_func.asm'''. '''Do not modify your hw4.asm''', since you will not be submitting your hw4.asm. A slightly different hw4.asm will be used to test your hw4_func.asm program, passing different values to your functions to make sure they work well. | ||
<br /> | <br /> | ||
+ | ==Note 2== | ||
<br /> | <br /> | ||
+ | When Moodle compares your output to the expected output, it uses the ''diff'' Linux command, which highlights differences between the two outputs by displaying lines that mismatch with a prefix. It uses a + sign in front of the lines from the expected output, and a - sign in front of the lines generated by the program just submitted. Lines that are the same in both the expected output and the output of the submitted program do not have a prefix. | ||
+ | <br /> | ||
+ | Below is an example that was generated by clicking on the '''debug''' button in Moodle, instead of '''evaluate''': | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | |||
+ | average = 7 | ||
+ | eax = 65 | ||
+ | ebx = 37 | ||
+ | - 3*eax - ebx + 2*ecx - 1 = 35 | ||
+ | ? ^ | ||
+ | + 3*eax - ebx + 2*ecx - 1 = 36 | ||
+ | ? ^ | ||
+ | - 3*eax - ebx + 2*ecx - 1 = 413 | ||
+ | ? ^ | ||
+ | + 3*eax - ebx + 2*ecx - 1 = 414 | ||
+ | ? ^ | ||
+ | 139 | ||
+ | 6 | ||
+ | 1 | ||
+ | 1 | ||
+ | 4 | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | * The lines that do not have a prefix in front of them are the same in both the student output and the expected output. The first 3 lines are good, and so are the last 5 lines. | ||
+ | * The lines with a + are the expected lines, and next to them are the ones with a - and are from the submitted program. The caret ^ shows where the difference is located. It looks like the submitted program generates an output that is 1 lower than it should be! | ||
+ | |||
+ | </showafterdate> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <showafterdate after="20151022 00:00" before="20151231 00:00"> | ||
+ | =Solution Program= | ||
+ | ::<source lang="asm"> | ||
+ | ;;; ; hw4_func.asm | ||
+ | ;;; ; D. Thiebaut | ||
+ | ;;; ; | ||
+ | |||
+ | |||
+ | ;;; ; extern functions that will be linked to this program | ||
+ | ;;; ; contained in 231Lib.asm | ||
+ | |||
+ | global someFunc, computeSum, computeAvg, swap, teller | ||
+ | |||
+ | ;;; ---------------------------------------------------- | ||
+ | ;;; someFunc: 3*eax - ebx + 2*ecx - 1 | ||
+ | ;;; ---------------------------------------------------- | ||
+ | someFunc: mov edx, eax | ||
+ | add eax, edx | ||
+ | add eax, edx | ||
+ | sub eax, ebx | ||
+ | add eax, ecx | ||
+ | add eax, ecx | ||
+ | sub eax, 1 | ||
+ | ret | ||
+ | |||
+ | computeSum: add eax, ebx | ||
+ | add eax, ecx | ||
+ | add eax, edx | ||
+ | ret | ||
+ | |||
+ | computeAvg: call computeSum | ||
+ | mov edx, 0 | ||
+ | mov ebx, 4 | ||
+ | div ebx | ||
+ | ret | ||
+ | |||
+ | swap: mov ecx, eax | ||
+ | mov eax, ebx | ||
+ | mov ebx, ecx | ||
+ | ret | ||
+ | |||
+ | section .data | ||
+ | x dd 0 | ||
+ | n20 dd 0 | ||
+ | n10 dd 0 | ||
+ | n5 dd 0 | ||
+ | n1 dd 0 | ||
+ | |||
+ | section .text | ||
+ | teller: mov dword[x], eax | ||
+ | mov ebx, 20 | ||
+ | mov edx, 0 | ||
+ | div ebx | ||
+ | mov dword[n20], eax | ||
+ | |||
+ | mov eax, edx | ||
+ | mov edx, 0 | ||
+ | mov ebx, 10 | ||
+ | div ebx | ||
+ | mov dword[n10], eax | ||
+ | |||
+ | mov eax, edx | ||
+ | mov edx, 0 | ||
+ | mov ebx, 5 | ||
+ | div ebx | ||
+ | mov dword[n5], eax | ||
+ | mov dword[n1], edx | ||
+ | |||
+ | mov eax, dword[n20] | ||
+ | mov ebx, dword[n10] | ||
+ | mov ecx, dword[n5] | ||
+ | mov edx, dword[n1] | ||
+ | ret | ||
+ | |||
+ | |||
+ | </source> | ||
+ | </showafterdate> | ||
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 15:25, 15 October 2015
--D. Thiebaut (talk) 19:11, 14 October 2015 (EDT)
This assignment is due on Wednesday, Oct. 21, at 11:55 p.m.
<showafterdate after="20151014 12:00" before="20151231 00:00">
Contents
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 and in hw4_func.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.
Notes
Note 1
- You are allowed to declare additional variables in your data section, if you need to. Just add a section .data block to your hw4_func.asm. Do not modify your hw4.asm, since you will not be submitting your hw4.asm. A slightly different hw4.asm will be used to test your hw4_func.asm program, passing different values to your functions to make sure they work well.
Note 2
When Moodle compares your output to the expected output, it uses the diff Linux command, which highlights differences between the two outputs by displaying lines that mismatch with a prefix. It uses a + sign in front of the lines from the expected output, and a - sign in front of the lines generated by the program just submitted. Lines that are the same in both the expected output and the output of the submitted program do not have a prefix.
Below is an example that was generated by clicking on the debug button in Moodle, instead of evaluate:
average = 7 eax = 65 ebx = 37 - 3*eax - ebx + 2*ecx - 1 = 35 ? ^ + 3*eax - ebx + 2*ecx - 1 = 36 ? ^ - 3*eax - ebx + 2*ecx - 1 = 413 ? ^ + 3*eax - ebx + 2*ecx - 1 = 414 ? ^ 139 6 1 1 4
- The lines that do not have a prefix in front of them are the same in both the student output and the expected output. The first 3 lines are good, and so are the last 5 lines.
- The lines with a + are the expected lines, and next to them are the ones with a - and are from the submitted program. The caret ^ shows where the difference is located. It looks like the submitted program generates an output that is 1 lower than it should be!
</showafterdate>
<showafterdate after="20151022 00:00" before="20151231 00:00">
Solution Program
;;; ; hw4_func.asm ;;; ; D. Thiebaut ;;; ; ;;; ; extern functions that will be linked to this program ;;; ; contained in 231Lib.asm global someFunc, computeSum, computeAvg, swap, teller ;;; ---------------------------------------------------- ;;; someFunc: 3*eax - ebx + 2*ecx - 1 ;;; ---------------------------------------------------- someFunc: mov edx, eax add eax, edx add eax, edx sub eax, ebx add eax, ecx add eax, ecx sub eax, 1 ret computeSum: add eax, ebx add eax, ecx add eax, edx ret computeAvg: call computeSum mov edx, 0 mov ebx, 4 div ebx ret swap: mov ecx, eax mov eax, ebx mov ebx, ecx ret section .data x dd 0 n20 dd 0 n10 dd 0 n5 dd 0 n1 dd 0 section .text teller: mov dword[x], eax mov ebx, 20 mov edx, 0 div ebx mov dword[n20], eax mov eax, edx mov edx, 0 mov ebx, 10 div ebx mov dword[n10], eax mov eax, edx mov edx, 0 mov ebx, 5 div ebx mov dword[n5], eax mov dword[n1], edx mov eax, dword[n20] mov ebx, dword[n10] mov ecx, dword[n5] mov edx, dword[n1] ret
</showafterdate>