Difference between revisions of "CSC231 Homework 3 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Preparation= <br /> ==Looping. Version 1== <br /> * Create the following program in your beowulf2 directory: <br /> <source lang="asm"> ;;; ; loop1.asm ;;; ; D. T...")
 
(Looping. Version 2)
Line 50: Line 50:
 
<br />
 
<br />
 
==Looping. Version 2==
 
==Looping. Version 2==
 +
<br />
 +
* This time we write the same program as the one above, but add some code to make it display "ecx = 10", "ecx = 9", etc. 
 +
* So the body of the loop will look something like this:
 +
<br />
 +
<source lang="asm">
 +
                mov    ecx, msg
 +
                mov    edx, MSGLEN
 +
                call    _printString
 +
</source>
 +
<br />
 +
:You should recognize the way _printString works: you pass the address of the string in ecx, the length of the string in edx, and you call the function.
 +
* The problem with this code is that it modifies ecx, which is our loop counter.  So we'd better save ecx before we execute this code and restore it right after.  We use a variable called '''temp''' for this purpose:
 +
<br />
 +
<source lang="asm">
 +
;;; in the data segment:
 +
temp        dd        0                    ; safe place to save ecx
 +
 +
;;; back in the code segment:
 +
 +
                mov    dword[temp], ecx
 +
                mov    ecx, msg
 +
                mov    edx, MSGLEN
 +
                call      _printString
 +
                mov    ecx, dword[temp]
 +
</source>
 
<br />
 
<br />

Revision as of 14:25, 29 September 2014

--D. Thiebaut (talk) 15:17, 29 September 2014 (EDT)


Preparation


Looping. Version 1


  • Create the following program in your beowulf2 directory:


;;; ; loop1.asm
;;; ; D. Thiebaut
;;; ;
;;; ; a simple program using a loop in which the ecx
;;; ; register is printed.
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ;     nasm -f elf loop1.asm
;;; ;     nasm -f elf 231Lib.asm
;;; ;     ld -melf_i3896 -o loop1 loop1.o 231Lib.o
;;; ;     ./loop1
;;; ; -------------------------------------------------------------------

extern _printDec                       ; the function that prints eax in decimal
extern _println                          ; the function that brings the cursor to the next line.

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

                section .text
                global  _start

_start:
                mov     ecx, 10
for:            mov     eax, ecx
                call    _printDec
                call    _println
                loop    for

;;;  exit()

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


  • Run it.
  • Verify that it displays the different values of ecx, the loop counter.


Looping. Version 2


  • This time we write the same program as the one above, but add some code to make it display "ecx = 10", "ecx = 9", etc.
  • So the body of the loop will look something like this:


                mov     ecx, msg
                mov     edx, MSGLEN
                call    _printString


You should recognize the way _printString works: you pass the address of the string in ecx, the length of the string in edx, and you call the function.
  • The problem with this code is that it modifies ecx, which is our loop counter. So we'd better save ecx before we execute this code and restore it right after. We use a variable called temp for this purpose:


;;; in the data segment:
temp         dd        0                    ; safe place to save ecx

;;; back in the code segment:

                mov     dword[temp], ecx
                mov     ecx, msg
                mov     edx, MSGLEN
                call      _printString
                mov     ecx, dword[temp]