Difference between revisions of "CSC231 Review Parameter Passing"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <source lang="asm"> </source> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> Category:CSC231")
 
 
Line 2: Line 2:
 
----
 
----
 
<source lang="asm">
 
<source lang="asm">
 +
 +
;;; reviewSumTwoVars.asm
 +
;;; D. Thiebaut
 +
;;;
 +
;;; in C++
 +
;;; ======
 +
;;;
 +
;;; int a = 2, b = 3, res = 0;
 +
;;; sum( a, b, &res );
 +
;;;
 +
;;; void sum( int x, int y, int* z ) {
 +
;;;  *z = x + y;
 +
;;; }
 +
;;;
 +
;;; same code in assembly:  look below
 +
;;;
 +
 +
%include "dumpRegs.asm"
 +
 +
section .data
 +
a dd 2
 +
b dd 3
 +
res dd 0
 +
 +
 +
section .text
 +
global _start
 +
_start:
 +
;;; example of passing by value and by reference via registers
 +
;;; ----------------------------------------------------------
 +
 +
mov eax, [a]
 +
mov ebx, [b]
 +
mov ecx, res
 +
call sum1
 +
 +
mov edx, [res]
 +
call dumpRegs
 +
 +
;;; example of passing by value and by reference via the stack
 +
;;; ----------------------------------------------------------
 +
mov dword[res], 0 ;reset what's in res
 +
 +
push dword [a]
 +
push dword [b]
 +
push dword res
 +
call sum2
 +
 +
mov edx, [res]
 +
call dumpRegs
 +
 +
;;; exit
 +
mov eax, 1
 +
mov ebx, 0
 +
int 0x80
 +
 +
;;; ----------------------------------------------------------
 +
;;; sum1: gets 2 values in eax and ecx, and stores their sum
 +
;;; at dword [ecx]
 +
;;; Does not modify registers
 +
;;; ----------------------------------------------------------
 +
sum1: mov [ecx], eax
 +
add [ecx], ebx
 +
ret
 +
 +
;;; ----------------------------------------------------------
 +
;;; sum2: gets 2 values in the stack and a reference and
 +
;;; stores the sum of the values at that reference.
 +
;;; Does not modify registers
 +
;;; ----------------------------------------------------------
 +
sum2: push ebp
 +
mov ebp, esp
 +
push eax
 +
push ebx
 +
 +
mov eax, [ebp + 12]
 +
add eax, [ebp + 16]
 +
mov ebx, [ebp + 8]
 +
mov [ebx], eax
 +
 +
pop ebx
 +
pop eax
 +
pop ebp
 +
ret 3*4
 +
  
 
</source>
 
</source>

Latest revision as of 10:32, 16 November 2012

--D. Thiebaut 10:31, 16 November 2012 (EST)


;;; reviewSumTwoVars.asm
;;; D. Thiebaut
;;;
;;; in C++
;;; ======
;;; 
;;; int a = 2, b = 3, res = 0;
;;; sum( a, b, &res );
;;;
;;; void sum( int x, int y, int* z ) {
;;;   *z = x + y;
;;; }
;;;
;;; same code in assembly:  look below
;;;

%include "dumpRegs.asm"

	section	.data
a	dd	2
b	dd	3
res	dd	0


	section	.text
	global	_start
_start:
;;; example of passing by value and by reference via registers
;;; ----------------------------------------------------------

	mov	eax, [a]
	mov	ebx, [b]
	mov	ecx, res
	call	sum1

	mov	edx, [res]
	call	dumpRegs

;;; example of passing by value and by reference via the stack
;;; ----------------------------------------------------------
	mov	dword[res], 0	;reset what's in res
	
	push	dword [a]
	push	dword [b]
	push	dword res
	call	sum2

	mov	edx, [res]
	call	dumpRegs

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

;;; ----------------------------------------------------------
;;; sum1: gets 2 values in eax and ecx, and stores their sum
;;; at dword [ecx]
;;; Does not modify registers
;;; ----------------------------------------------------------
sum1:	mov	[ecx], eax
	add	[ecx], ebx
	ret

;;; ----------------------------------------------------------
;;; sum2: gets 2 values in the stack and a reference and 
;;; stores the sum of the values at that reference.
;;; Does not modify registers
;;; ----------------------------------------------------------
sum2:	push	ebp
	mov	ebp, esp
	push	eax
	push	ebx
	
	mov	eax, [ebp + 12]
	add	eax, [ebp + 16]
	mov	ebx, [ebp + 8]
	mov	[ebx], eax

	pop	ebx
	pop	eax
	pop	ebp
	ret	3*4