CSC231 Homework 4 2015

From dftwiki3
Revision as of 18:32, 14 October 2015 by Thiebaut (talk | contribs)
Jump to: navigation, search

--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">

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


Note


  • You are allowed to add additional variables to your data section, if you need to. Just add a data section to hw4_func.asm to do so.


Submission


  • Submit your hw4_func.asm file (only, no need for hw4.asm) on Moodle, in the HW 4 PB 1 section.



</showafterdate>