Difference between revisions of "CSC231 Homework 5 2014"

From dftwiki3
Jump to: navigation, search
(Problem #1)
(f.asm)
Line 106: Line 106:
 
                 add    eax, eax
 
                 add    eax, eax
 
                 pop    ebp
 
                 pop    ebp
                 ret
+
                 ret     4   
  
 
</source>
 
</source>
 
<br />
 
<br />
 +
 
==Assembly/Link Steps==
 
==Assembly/Link Steps==
 
<br />
 
<br />

Revision as of 07:33, 29 October 2014

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


Preparation


The two programs below illustrate how you would put a function, f(), in a separate file, and call it from a main() program kept in another file.
The specific details that are important and that make the system work:

  • there is only one global _start label, and it is in main. The linker (ld) will complain if there are more than one _start label, as it is where Linux will start running your program.
  • The label f in f.asm must be global, so that example.asm can access it.
  • The label f in example.asm must be extern, so that nasm will not complain that it is not finding the label f used in the call instruction.
  • Both example.asm and f.asm should be assembled separately, and linked together with the 231Lib library.


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     4


Assembly/Link Steps


 nasm -f elf example.asm
 nasm -f elf f.asm
 nasm -f efl 231Lib.asm
 ld -melf_i386 example.o  f.o  231Lib.o  -o example

Output


200
6
1602


Advantages


The advantage of this setup is that if we put several functions in a separate file, they can become a nice collection of utility functions, and serve as a library. This is what we are doing with 231Lib.asm, containing _printDec, _println, and _printString.
The second advantage is that I can test your functions by linking them to my own test program. My test program can easily exercise your functions, and subject them to different conditions, and see whether the functions modify registers, and which ones.

Problem #1


Write a program called Hw5_1.asm that contains 1 function called
f1
that will compute f1( x ) = (3 * x) - 1, and returns the result in eax.

Use f.asm above as an example. Your program cannot contain a main program with a label _start. You have to create it on your own main program to test your function.

Your function cannot modify any of the registers besides eax.

You only submit Hw5_1.asm to Moodle, and keep your test program.

Problem #2


Write a program called Hw5_2.asm that contains 1 function called f2 that computes f2( x ) = x + (x-1) + (x-2) + ... + 2 + 1, and returns the result in eax. We assume that x will always be strictly positive, and your function does not have to deal with negative numbers.

Your function cannot modify any of the registers besides eax.

Submit your program to Moodle, and keep your test program.

Problem #3


Write a program called Hw5_3.asm that contains 2 functions, f3 and f4, defined as follows:

  • f3( x ) = 2 x + 10
  • f4( x ) = 3 f3( x ) + f3( x-1 )

Your functions cannot modify any of the registers besides eax.

Submit your program to Moodle, and keep your test program.

Problem #4


Write a program called Hw5_4.asm that contains 2 functions, f5, and f6, defined as follows:

  • f5( x ) = x + (x-1) + (x-2) + ... + 2 + 1
  • f6( x ) = f5( x ) + f5( x-1 ) + f5( x-2 ) + ... + f5( 2 ) + f5( 1 )

Your functions cannot modify any of the registers besides eax.

Submit your program to Moodle, and keep your test program.