CSC231 SumProcPrint.asm

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 12:55, 14 October 2015 (EDT)


Source


;;; ; add2Ints.asm
;;; ; D. Thiebaut
;;; ;
;;; ; Demo of a function that gets parameters through
;;; ; registers and returns computed value in eax
;;; ; To assemble, link, and run:
;;; ;     nasm -f elf  add2Ints.asm
;;; ;     ld -melf_i386 -o add2Ints add2Ints.o 231Lib.o
;;; ;     ./add2Ints
;;; ;                

;;;  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
a               dd      5
b               dd      8
        
msg             db      "sum = "
MSGLEN          equ     $-msg        

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

;;; print message
                ;; print "sum = "
                mov     ecx, msg
                mov     edx, MSGLEN
                call    _printString

                ;; compute a + b
	        mov	eax, dword[a]
		mov	ebx, dword[b]
                call    sum             ; eax <- a+b
        
                call    _printDec       ; print eax
                call    _println        ; go next line

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



Output


sum = 13