Difference between revisions of "CSC231 SumProc2.asm"

From dftwiki3
Jump to: navigation, search
(New page: Back to Class Page ---- <code><pre> </pre></code>)
 
Line 3: Line 3:
  
 
<code><pre>
 
<code><pre>
 +
;;; sumproc2.asm
 +
;;; D. Thiebaut CSC231
 +
;;; second of a series of programs illustrating how to pass and receive
 +
;;; information to and from a function in assembly.
 +
;;;
 +
;;; This and all associated programs simply add the contents of
 +
;;; two variables a and b and store the resulting sum in a variable
 +
;;; called result (all are double words)
 +
;;;
 +
;;; this version passes a and b by value through the stack. the
 +
;;; sum is passed back in eax.
 +
 +
 +
%assign SYS_EXIT 1
 +
%assign SYS_WRITE 4
 +
%assign STDOUT 1
 +
 +
;; -------------------------
 +
;; data segment
 +
;; -------------------------
 +
 +
section .data
 +
a dd 1234 ; operand 1
 +
b dd 5555 ; operand 2
 +
result dd 0 ; where the sum will be stored
 +
 +
;; -------------------------
 +
;; code area
 +
;; -------------------------
 +
 +
section .text
 +
global _start
 +
 +
_start:
 +
push dword [a] ; copy a in the stack
 +
push dword [b] ; copy b in the stack
 +
call sum
 +
mov dword [result], eax ; get sum back in eax
 +
 +
 +
mov eax,SYS_EXIT
 +
mov ebx,0
 +
int 0x80 ; final system call
 +
 +
;;; -------------------------------------------------------
 +
;;; sum function
 +
;;; gets 2 dwords from stack and stores result in eax
 +
;;; registers modified: eax
 +
;;;
 +
;;; stack looks like this after first 2 instructions:
 +
;;;
 +
;;;        +----------+
 +
;;;        |copy of a | <-- ebp+12
 +
;;;        +----------+
 +
;;;        |copy of b | <-- ebp+8
 +
;;;        +----------+
 +
;;;        |retrn addr| <-- ebp+4  (plus 4 because double words!)
 +
;;;        +----------+
 +
;;;        |"old" ebp | <-- ebp <-- esp
 +
;;;        +----------+
 +
;;;        |          |
 +
;;; -------------------------------------------------------
 +
sum: push ebp ; save "old" ebp
 +
mov ebp,esp ; make ebp point to stack frame
 +
 +
mov eax,dword [ebp+8]  ; get copy of b
 +
add eax,dword [ebp+12] ; add copy of a to it
 +
 +
pop ebp ; restore "old" ebp
 +
ret 8 ; get rid of 8 bytes of storage in stack
  
 
</pre></code>
 
</pre></code>

Revision as of 12:51, 3 November 2008

Back to Class Page


;;; sumproc2.asm
;;; D. Thiebaut CSC231
;;; second of a series of programs illustrating how to pass and receive
;;; information to and from a function in assembly.
;;;
;;; This and all associated programs simply add the contents of
;;; two variables a and b and store the resulting sum in a variable
;;; called result (all are double words)
;;;
;;; this version passes a and b by value through the stack. the
;;; sum is passed back in eax.


%assign SYS_EXIT	1
%assign	SYS_WRITE	4
%assign	STDOUT		1

	;; -------------------------
	;; data segment
	;; -------------------------

	section	.data
a	dd	1234		; operand 1
b	dd	5555		; operand 2
result	dd	0		; where the sum will be stored

	;; -------------------------
	;; code area
	;; -------------------------

	section	.text
	global	_start

_start:
	push	dword [a]	; copy a in the stack
	push	dword [b]	; copy b in the stack
	call	sum
	mov	dword [result], eax ; get sum back in eax


	mov	eax,SYS_EXIT
	mov	ebx,0
	int	0x80		; final system call

;;; -------------------------------------------------------
;;; sum function
;;; gets 2 dwords from stack and stores result in eax
;;; registers modified:	 eax
;;;
;;; stack looks like this after first 2 instructions:
;;;
;;;         +----------+
;;;         |copy of a | <-- ebp+12
;;;         +----------+
;;;         |copy of b | <-- ebp+8
;;;         +----------+	
;;;         |retrn addr| <-- ebp+4  (plus 4 because double words!)
;;;         +----------+	
;;;         |"old" ebp | <-- ebp <-- esp
;;;         +----------+
;;;         |          | 
;;; -------------------------------------------------------
sum:	push	ebp		; save "old" ebp
	mov	ebp,esp		; make ebp point to stack frame

	mov	eax,dword [ebp+8]  ; get copy of b
	add	eax,dword [ebp+12] ; add copy of a to it 
	
	pop	ebp		; restore "old" ebp
	ret	8		; get rid of 8 bytes of storage in stack