Difference between revisions of "CSC231 Crash Course Solution 7"

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>
 +
;;; prog7.asm
 +
;;; D. Thiebaut
 +
;;; solution for Exercise #7
 +
;;;
 +
;;; Assemble, link and run as follows:
 +
;;;  nasm -f elf asm_io.asm        (do this only once)
 +
;;;  nasm -f elf prog7.asm
 +
;;;  gcc -o prog7 driver.c prog7.o asm_io.o
 +
;;;  ./prog7
 +
;;;
 +
%include "asm_io.inc"
 +
       
 +
%assign SYS_EXIT        1
 +
 +
        ;; -------------------------
 +
        ;; data segment
 +
        ;; -------------------------
 +
        section .data
 +
A      dd      1, 0, 2, 0, 5
 +
B      dd      1, 0, 0, 3, 1, 2, 0, 0, 9, 7
 +
 +
        ;; -------------------------
 +
        ;; code area
 +
        ;; -------------------------
 +
        section .text
 +
        global  asm_main
 +
;;; ----------------------------------------------------------------
 +
;;; printNotZero: receives the address of an array in esi, and the
 +
;;;              number of items in ecx, and prints only the
 +
;;;              items that are not 0
 +
;;; ----------------------------------------------------------------
 +
printNotZero:
 +
 +
for:    mov    eax, [esi]
 +
        cmp    eax, 0
 +
        je      done
 +
        call    print_int
 +
        call    print_nl
 +
done:  add    esi, 4
 +
        loop    for
 +
        ret
 +
       
 +
       
 +
;;; ----------------------------------------------------------------
 +
;;;                M A I N  P R O G R A M
 +
;;; ----------------------------------------------------------------
 +
asm_main:
 +
        mov    esi, A
 +
        mov    ecx, 5
 +
        call    printNotZero
 +
 +
        mov    ecx, 3
 +
for2:  call    print_nl              ; print 3 blank lines
 +
        loop    for2
 +
       
 +
        mov    esi, B
 +
        mov    ecx, 10
 +
        call    printNotZero
 +
 +
        ;; return to C program
 +
 +
        ret
  
 
</pre></code>
 
</pre></code>

Latest revision as of 15:13, 4 March 2011

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


;;; prog7.asm
;;; D. Thiebaut
;;; solution for Exercise #7
;;; 
;;; Assemble, link and run as follows:
;;;   nasm -f elf asm_io.asm        (do this only once)
;;;   nasm -f elf prog7.asm
;;;   gcc -o prog7 driver.c prog7.o asm_io.o
;;;   ./prog7
;;; 
%include "asm_io.inc"
        
%assign SYS_EXIT        1

        ;; -------------------------
        ;; data segment
        ;; -------------------------
        section .data
A       dd      1, 0, 2, 0, 5
B       dd      1, 0, 0, 3, 1, 2, 0, 0, 9, 7

        ;; -------------------------
        ;; code area
        ;; -------------------------
        section .text
        global  asm_main
;;; ----------------------------------------------------------------
;;; printNotZero: receives the address of an array in esi, and the
;;;               number of items in ecx, and prints only the
;;;               items that are not 0
;;; ----------------------------------------------------------------
printNotZero:

for:    mov     eax, [esi]
        cmp     eax, 0
        je      done
        call    print_int
        call    print_nl
done:   add     esi, 4
        loop    for
        ret
        
        
;;; ----------------------------------------------------------------
;;;                M A I N   P R O G R A M
;;; ----------------------------------------------------------------
asm_main:
        mov     esi, A
        mov     ecx, 5
        call    printNotZero

        mov     ecx, 3
for2:   call    print_nl              ; print 3 blank lines
        loop    for2
        
        mov     esi, B
        mov     ecx, 10
        call    printNotZero

        ;; return to C program

        ret