CSC231 Homework 4 2015

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

--D. Thiebaut (talk) 19:11, 14 October 2015 (EDT)


Page under construction!

UnderConstruction.jpg

Preparation


  • 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