Difference between revisions of "CSC231 Crash Course Solution 4"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <code><pre> </pre></code> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> Category:CSC231")
 
 
Line 2: Line 2:
 
----
 
----
 
<code><pre>
 
<code><pre>
 +
;;; prog4.asm
 +
;;; D. Thiebaut
 +
;;; a simple demo program to add 2 numbers
 +
;;; together, and store the sum in a 3rd variable
 +
;;;
 +
;;; Assemble, link and run as follows:
 +
;;;  nasm -f elf asm_io.asm        (do this only once)
 +
;;;  nasm -f elf prog4.asm
 +
;;;  gcc -o prog4 driver.c prog4.o asm_io.o
 +
;;;  ./prog4
 +
;;;
 +
%include "asm_io.inc"
 +
       
 +
%assign SYS_EXIT        1
 +
 +
        ;; -------------------------
 +
        ;; data segment
 +
        ;; -------------------------
 +
        section .data
 +
table  dd      1, 1, 0, 0, 0
 +
 +
 +
        ;; -------------------------
 +
        ;; code area
 +
        ;; -------------------------
 +
        section .text
 +
        global  asm_main
 +
asm_main:     
 +
        mov    esi, table      ;esi points to table
 +
        mov    eax, [esi]      ;get first fib
 +
        add    eax, [esi+4]    ;add 2nd fib to eax
 +
        mov    [esi+8], eax    ;store 3rd fib in array
 +
 +
        add    esi, 8          ;make esi point to 3rd fib
 +
        add    eax, [esi-4]    ;add 2nd fib to 3rd fib
 +
        mov    [esi+4], eax    ;store 4th fib in array
 +
 +
        add    esi, 4          ;make esi point to 4th fib
 +
        add    eax, [esi-4]    ;add 3rd fib to 4th fib
 +
        mov    [esi+4],eax    ;store 5th fib in array
 +
 +
;;; print the contents of the array
 +
       
 +
        mov    eax, [table]
 +
        call    print_int
 +
        call    print_nl
 +
 +
        mov    eax, [table+4]
 +
        call    print_int
 +
        call    print_nl
 +
 +
        mov    eax, [table+8]
 +
        call    print_int
 +
        call    print_nl
 +
 +
        mov    eax, [table+12]
 +
        call    print_int
 +
        call    print_nl
 +
 +
        mov    eax, [table+16]
 +
        call    print_int
 +
        call    print_nl
 +
       
 +
        ;; return to C program
 +
 +
        ret
  
 
</pre></code>
 
</pre></code>

Latest revision as of 15:11, 4 March 2011

--D. Thiebaut 15:09, 4 March 2011 (EST)


;;; prog4.asm
;;; D. Thiebaut
;;; a simple demo program to add 2 numbers
;;; together, and store the sum in a 3rd variable
;;; 
;;; Assemble, link and run as follows:
;;;   nasm -f elf asm_io.asm        (do this only once)
;;;   nasm -f elf prog4.asm
;;;   gcc -o prog4 driver.c prog4.o asm_io.o
;;;   ./prog4
;;; 
%include "asm_io.inc"
        
%assign SYS_EXIT        1

        ;; -------------------------
        ;; data segment
        ;; -------------------------
        section .data
table   dd      1, 1, 0, 0, 0


        ;; -------------------------
        ;; code area
        ;; -------------------------
        section .text
        global  asm_main
asm_main:       
        mov     esi, table      ;esi points to table
        mov     eax, [esi]      ;get first fib
        add     eax, [esi+4]    ;add 2nd fib to eax
        mov     [esi+8], eax    ;store 3rd fib in array

        add     esi, 8          ;make esi point to 3rd fib
        add     eax, [esi-4]    ;add 2nd fib to 3rd fib
        mov     [esi+4], eax    ;store 4th fib in array

        add     esi, 4          ;make esi point to 4th fib
        add     eax, [esi-4]    ;add 3rd fib to 4th fib
        mov     [esi+4],eax     ;store 5th fib in array 

;;; print the contents of the array
        
        mov     eax, [table]
        call    print_int
        call    print_nl

        mov     eax, [table+4]
        call    print_int
        call    print_nl

        mov     eax, [table+8]
        call    print_int
        call    print_nl

        mov     eax, [table+12]
        call    print_int
        call    print_nl

        mov     eax, [table+16]
        call    print_int
        call    print_nl
        
        ;; return to C program

        ret