Difference between revisions of "CSC231 Crash Course Solution 2"

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>
 +
;;; prog2.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 prog2.asm
 +
;;;  gcc -o prog2 driver.c prog2.o asm_io.o
 +
;;;  ./prog2
 +
;;;
 +
%include "asm_io.inc"
 +
       
 +
%assign SYS_EXIT        1
 +
 +
        ;; -------------------------
 +
        ;; data segment
 +
        ;; -------------------------
 +
        section .data
 +
a      dd      3
 +
b      dd      5
 +
result  dd      0
 +
 +
        ;; -------------------------
 +
        ;; code area
 +
        ;; -------------------------
 +
        section .text
 +
        global  asm_main
 +
asm_main:     
 +
        mov    eax,[a]
 +
        add    eax,[b]
 +
        mov    [result],eax
 +
       
 +
        mov    eax,[result]          ; pass result to print_int
 +
        call    print_int            ; and print value
 +
 +
        call    print_nl              ; call the function that prints
 +
                                      ; a new blank line
 +
 +
        ;; return to C program
 +
 +
        ret
  
 
</pre></code>
 
</pre></code>

Latest revision as of 15:11, 4 March 2011

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


;;; prog2.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 prog2.asm
;;;   gcc -o prog2 driver.c prog2.o asm_io.o
;;;   ./prog2
;;; 
%include "asm_io.inc"
        
%assign SYS_EXIT        1

        ;; -------------------------
        ;; data segment
        ;; -------------------------
        section .data
a       dd      3
b       dd      5
result  dd      0

        ;; -------------------------
        ;; code area
        ;; -------------------------
        section .text
        global  asm_main
asm_main:       
        mov     eax,[a]
        add     eax,[b]
        mov     [result],eax
        
        mov     eax,[result]          ; pass result to print_int
        call    print_int             ; and print value

        call    print_nl              ; call the function that prints
                                      ; a new blank line

        ;; return to C program

        ret