CSC231 Functions and the Stack
--D. Thiebaut 10:41, 31 October 2012 (EDT)
Contents
A Shell File to Assemble and Link Assembly Programs
- The text file below can be created with emacs. Call it nasmld (or some other name of your choosing):
#! /bin/bash
# nasmld
# a script to assemble and link assembly files
nasm -f elf -F stabs $@.asm
ld -o $@ -melf_i386 $@.o
./$@
- make sure you make your script executable by using this command:
chmod a+rx nasmld
Program 1: funcExample.asm
;;; ------------------------------------
;;; funcExample.asm
;;; prints 2 strings using functions
;;; ------------------------------------
section .data
Hello db "Hello "
There db "there!", 10, 10, 10
HelloLen equ $-Hello
section .text
global _start
_start:
;;; ------------------------------------
;;; main program
;;; ------------------------------------
;;; print message
mov ecx, Hello
mov edx, HelloLen
call printString
mov ecx, There
mov edx, 9
call printString
;;; exit
mov ebx, 0
mov eax, 1
int 0x80
;;; ------------------------------------
;;; printString: prints string whose
;;; address is in ecx, and length in edx
;;; ------------------------------------
printString:
mov eax, 4
mov ebx, 1
int 0x80
ret
Program 2: funcExample2.asm
;;; ----------------------------------
;;; funcExample2.asm
;;; prints 2 strings with lines of stars
;;; between them
;;; ----------------------------------
section .data
Hello db "Hello "
There db "there!", 10, 10, 10
HelloLen equ $-Hello
line db "****************************", 10
lineLen equ $-line
section .text
global _start
_start:
;;; ----------------------------------
;;; main program
;;; ----------------------------------
call printLine
mov ecx, Hello
mov edx, HelloLen
call printString
call printLine
mov ecx, There
mov edx, 9
call printString
call printLine
;;; exit
mov ebx, 0
mov eax, 1
int 0x80
;;; ----------------------------------
;;; printString: Prints a string whose
;;; address is in ecx and length in edx
;;; ----------------------------------
printString: mov eax, 4
mov ebx, 1
int 0x80
ret
;;; ----------------------------------
;;; printLine: prints a line of stars.
;;; ----------------------------------
printLine: mov ecx, line
mov edx, lineLen
call printString
ret