/*
 * COSC 065 - Hardware Systems
 * Fall 2005
 * D.W. Brylow 
 * Lecture 25 demonstration
 * Intel x86 assembly demo.
 */

.section .rodata
FMT:	.string "%d\n"
	
.section .text
.globl main

/* int main()
 *  {
 *       foo(4);
 *  }
 */ 
	
main:
	pushl	%ebp
	movl	%esp, %ebp

	pushl	$4
	call	foo

	pushl	%eax
	pushl	$FMT
	call	printf
	
	movl	%ebp, %esp
	popl	%ebp
	ret

		
/*  int foo(int x)
 *  {
 *       int y;
 *       int z;
 *       y = 5;
 *       z = x + y;
 *       return z;
 *  }
 */

foo:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	
	movl	$5, -8(%ebp)
	movl	-8(%ebp), %eax
	addl	8(%ebp), %eax
	movl	%eax, -4(%ebp)
	movl	-4(%ebp), %eax

	movl	%ebp, %esp
	popl	%ebp
	ret

