Difference between revisions of "CSC231 Homework 3 2014"

From dftwiki3
Jump to: navigation, search
(Problem #3: adding an input)
(Problem #3: adding an input)
Line 210: Line 210:
 
</source>
 
</source>
 
<br />
 
<br />
: In the last example, the prompt generated by the program is '''&gt;''', and the user input is '''10'''.  The program then loops 10 times.
+
: In the last example, the prompt generated by the program is '''&gt;''' followed by a space, and the user input is '''10'''.  The program then loops 10 times.

Revision as of 14:52, 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]


  • Update your program with the new code, indicate that _printString is an extern function, and assemble, link, and run your new program.
  • Verify that its output looks like this:


ecx = 10
ecx = 9
ecx = 8
ecx = 7
ecx = 6
ecx = 5
ecx = 4
ecx = 3
ecx = 2
ecx = 1


Looping. Version 3


  • Now try this program


;;; ; loop3.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
extern _println        
extern _printString


;;;  ------------------------------------------------------------
;;;  data areas
;;;  ------------------------------------------------------------

                section .data
Array           dd      1, 10, 20, 50, 100, 150, 200, 500, 1000
ARRAYLEN        equ     ($-Array)/4     ; the number of dwords in Array
temp            dd      0               ; a place to save ecx when needed
        
;;;  ------------------------------------------------------------
;;;  code area
;;;  ------------------------------------------------------------

                section .text
                global  _start

_start:


                mov     ecx, ARRAYLEN
                mov     ebx, Array
for:      

;;; display the integer at [ebx]
                mov     eax, dword[ebx]
                add     ebx, 4         ; make ebx point to next int in array
                call    _printDec
                call    _println
                
                loop    for

;;;  exit

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


  • Read it carefully. Can you predict what it will output?
  • Assemble, link, and run it.
  • Verify that your intuition was correct. If it was not, make sure you understand why.
  • You are now ready for the assignment.


Problem #1: warming up


  • Write an assembly program called hw3_1.asm that uses a loop to print all the numbers from 1 to 20, in increasing order, one per line.


  • Submit it to Moodle.

Problem #2: adding a string


  • Write an assembly program called Hw3_2.asm that uses a loop to prints ecx in the following way:


1. ecx = 20
2. ecx = 19
3. ecx = 18
4. ecx = 17
...         
20. ecx = 1


(I replaced a large number of lines using ellipses in the list above.)


Problem #3: adding an input


  • Your program should be called Hw3_3.asm, and is an extension of Hw3_2.asm.
  • It works the same way Hw3_2 operates, but instead of looping 20 times, the program uses a number input by the user to control the loop.
  • Example:


> 4
1. ecx = 4
2. ecx = 3
3. ecx = 2
4. ecx = 1


or, another example:


> 10
1. ecx = 10
2. ecx = 9
3. ecx = 8
4. ecx = 7
5. ecx = 6
6. ecx = 5
7. ecx = 4
8. ecx = 3
9. ecx = 2
10. ecx = 1


In the last example, the prompt generated by the program is > followed by a space, and the user input is 10. The program then loops 10 times.