Courses/Computer Science/CPSC 601.29.ISSA/20110128CodeSession

From wiki.ucalgary.ca
Jump to: navigation, search

Makefile:

  [michael@xorenduex lectures]$ cat Makefile 
  all: mywrite
  
  mywrite: mywrite.asm
  	nasm -f elf mywrite.asm
  	gcc -Wall -s -nostdlib mywrite.o
  
  clean:
  	@/bin/echo "nothing to do"
  [michael@xorenduex lectures]$ 


Assembly Source:

[michael@xorenduex lectures]$ cat mywrite.asm 
	;; mywrite.asm 
	;; goal is to invoke write system call
	;; write( fd,    buf,          size)
	;; eax/4  ebx/1  ecx/greeting  edx/5
BITS 32
GLOBAL _start
SECTION .data
greeting	db	'hello'
SECTION .text
_start:	
	mov	eax, 4		;sys_write is 4 
	mov	ebx, 1		;stdout (default file desc. supplied by OS)
	mov	ecx, greeting	;move address of label in .data to ecx
	mov	edx, 5		;5 byte
	int	0x80		;invoke write(2)
	mov	ebx, eax	;save ret val (eax) to ebx (arg to exit)
	mov	eax, 1		;sys_exit is 1
	int	0x80		;invoke exit(2)
[michael@xorenduex lectures]$