CSC231 Homework 4 2017

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 11:19, 26 February 2017 (EST)


<showafterdate after="20170227 11:00" before="20170601 00:00">


Problem 1


Write an assembly language program that prompts the user for a amount of money and outputs the number of $20-bills, $10-bills, $5-bills, and $1-bills that a teller machine will return, if this was the amount to be withdrawn.

Here is an example of how to assemble and run the solution program:

nasm -f elf teller.asm
ld -melf_i386 -o teller teller.o 231Lib.o
./teller
amount? 139

6 
1
1
4

  • The teller program prints "amount? " with a space at the end.
  • The user enter 139, and presses ENTER.
  • The program displays a blank line. This line is very important and required by the auto-grader.
  • The program then computes the number of $20-bills in $139: 6, and prints 6 followed by a new line.
  • The program then computes the number of $10 in the $19 left over, which is 1. It prints 1, followed by a new line.
  • It then computes the number of $5 in the $9 left over, or 1. It prints 1 and a new line.
  • Finally it prints the number of $1 in the $4 left over: 4. It prints 4 followed by a new line. Do not forget this last new line!

Your program should work in the exact same way. It should output no extra spaces. The prompt is "amount? ", have your program display the same word. The output is 1 blank line followed by 4 lines with only 1 number per line, and one final new line at the end.

Call your program teller.asm and submit it to the auto-grader. It will test it with 3 different amounts that result in 4 different sets of numbers. Your program will only be tested with positive numbers less than 1000.

Requirements


  • Make sure you use 32-bit ints to store the amount, as well as the number of $20 bills, number of $10 bills, number of $5 bills, and number of $1 bills.


Debugging Help


The 231Lib.asm (available here) library contains a nice function called _printRegs. You can call it in the middle of your program to get a listing of the contents of all registers. Note, it will also display ESI and EDI which we haven't seen yet.

Example:
              mov      edx, 0
              mov      eax, 20
              mov      ebx, 7
              div        ebx
              call        _printRegs


The output will be:

eax 00000002 2 2
ebx 00000007 7 7
ecx 00000000 0 0
edx 00000006 6 6
edi 00000000 0 0
esi 00000000 0 0

indicating that 20/7 has for quotient 2 (in eax) and remainder 6 (in edx). The output shows the contents of the registers in hex, as unsigned ints, and as signed ints.


Problem 2


Document your teller program well, and submit it as the answer to Question 2 on Moodle. In this question your program will be checked for style and documentation, and the grade will reflect only the code's readability. This way you will get a grade for correctness, and a grade for style.

Problem 3


Answer the multiple choice questions on Moodles that deal with number conversion in different bases. </showafterdate>


<showafterdate after="20170308 12:00" before="20170701">

Solutions


Problem 1

;;; ; teller.asm
;;; ; D. Thiebaut
;;; ;
;;; ; Demo of a teller machine program.
;;; ; To assemble, link, and run:
;;; ;     nasm -f elf  teller.asm
;;; ;     ld -melf_i386 -o teller teller.o 231Lib.o
;;; ;     ./teller
;;; ;                

;;;  extern functions that will be linked to this program
;;; ; contained in 231Lib.asm

extern  _printDec
extern  _printString
extern  _println
extern  _getInput

;;; ------------------------------------------------------
;;; DATA SECTION
;;; ------------------------------------------------------        
                section .data
amount          dd      139
no20s           dd      0
no10s           dd      0
no5s            dd      0
no1s            dd      0

msg             db      "amount? "
MSGLEN          equ     $-msg        

;;; ------------------------------------------------------
;;; CODE SECTION
;;; ------------------------------------------------------        
                section .text
                global  _start
        
_start: 

;;; get amount from the user
                mov     ecx, msg
                mov     edx, MSGLEN
                call    _printString
                call    _getInput
                mov     dword[amount], eax
        	call	_println
	
;;; Break down amount in 20s, 10s, 5s, and 1s
                mov     eax, dword[amount]
                mov     edx, 0

                mov     ebx, 20
                mov     eax, dword[amount]
                call    break
                mov     dword[no20s], eax

                mov     ebx, 10
                mov     eax, dword[amount]
                call    break
                mov     dword[no10s], eax
        
                mov     ebx, 5
                mov     eax, dword[amount]
                call    break
                mov     dword[no5s], eax
                mov     eax, dword[amount]
                mov     dword[no1s], eax

                mov     eax, dword[no20s]
                call    _printDec
                call    _println
                mov     eax, dword[no10s]
                call    _printDec
                call    _println
                mov     eax, dword[no5s]
                call    _printDec
                call    _println
                mov     eax, dword[no1s]
                call    _printDec
                call    _println
        

;;; exit
                mov     ebx, 0
                mov     eax, 1
                int     0x80

;;; sum: gets 2 ints in eax and ebx and returns the sum in
;;; eax.
sum:            add     eax, ebx,
                ret

;;; break: gets amount in eax, divider in ebx, divides,
;;; and puts remainder in [amount] and quotient in eax
break:          mov     edx, 0
                div     ebx
                mov     dword[amount], edx
                ret

Problem 3

------------------------------
1 = 1 1
15 = f 1111
32 = 20 100000
33 = 21 100001
65 = 41 1000001
65534 = fffe 1111111111111110
------------------------------
11110000 240
1111 15
1001100110011001 39321
10010 18
------------------------------
0x3333 0x6666 a+b =  9999
0x3333 0x7777 a+b =  aaaa
0xFFFF 0xAAAA a+b =  1aaa9


</showafterdate>