CSC231 Homework 5 2014

From dftwiki3
Revision as of 06:30, 29 October 2014 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Preparation= <br /> ==Example.asm== <br /> ::<source lang="asm"> ;;; ; example.asm ;;; ; D. Thiebaut ;;; ; ;;; ; A simple program to demonstrate how to link a mai...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut (talk) 07:30, 29 October 2014 (EDT)


Preparation


Example.asm


;;; ; example.asm
;;; ; D. Thiebaut
;;; ;
;;; ; A simple program to demonstrate how to link a main
;;; ; program and an extern function.
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ;     nasm -f elf -F  stabs example.asm
;;; ;     nasm -f elf -F  stabs f.asm
;;; ;     ld -melf_i3896 -o example example.o f.o 231Lib.o
;;; ;     ./example
;;; ; -------------------------------------------------------------------


;;;  ------------------------------------------------------------
;;;  data areas
;;;  ------------------------------------------------------------

                section .data
a               dd      100
b               dd      200
c               dd      300
result          dd      0

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

                section .text
                global  _start
extern          _printDec
extern          _println
extern          f
        
_start:
;;; compute f( a ) = 2*a
                push    dword[a]
                call    f
                call    _printDec
                call    _println
        
;;; compute f( 3 ) = 2*3 = 6
                push    dword 3 
                call    f
                call    _printDec
                call    _println

;;; compute f( b + f (c) + 1 )
                push    dword[c]
                call    f
                inc     eax
                add     eax, dword[b]
                push    eax
                call    f
                call    _printDec
                call    _println
                
       
;;;  exit()

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


f.asm


;;; ; f.asm
;;; ; D. Thiebaut
;;; ; A simple program with one global function that computes
;;; ; f(a) = 2*a
;;; ;
;;; ; -----------------------------------------------------------


                section .text
                global  f

;;; ;------------------------------------------------------------
;;; ; f(x): a function that receives a parameter through
;;; ; the stack and that computes y = 2*x
;;; ; x is at [ebp+8]
;;; ; returns the result in eax.
;;; ; does not modify any of the other registers (besides eax)
;;; ;------------------------------------------------------------
f:              push    ebp
                mov     ebp, esp
                mov     eax, dword[ebp+8]
                add     eax, eax
                pop     ebp
                ret