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...")
 
 
(29 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 15:17, 29 September 2014 (EDT)
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 15:17, 29 September 2014 (EDT)
 
----
 
----
 +
<br />
 +
<bluebox>
 +
This assignment deals with loop that use the ecx register and the '''loop''' instruction, and with '''indirect''', and '''based indexed''' addressing modes.  This assignment is '''due Thursday, Oct. 9th, 2014, at 11:55 p.m.'''  The first 3 loop sections are just for you to review loops, and do not need to be submitted.
 +
</bluebox>
 +
<br />
 
=Preparation=
 
=Preparation=
 +
<br />
 +
Go through the 3 different loop sections below to get ready for the homework problems for this week.
 
<br />
 
<br />
 
==Looping. Version 1==
 
==Looping. Version 1==
Line 7: Line 14:
 
* Create the following program in your beowulf2 directory:
 
* Create the following program in your beowulf2 directory:
 
<br />
 
<br />
<source lang="asm">
+
::<source lang="asm">
 
;;; ; loop1.asm
 
;;; ; loop1.asm
 
;;; ; D. Thiebaut
 
;;; ; D. Thiebaut
Line 51: Line 58:
 
==Looping. Version 2==
 
==Looping. Version 2==
 
<br />
 
<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 />
 +
* Update your program with the new code, indicate that '''_printString''' is an '''extern''' function, and assemble, link, and run your new program.
 +
* <font color="magenta">Verify that its output looks like this:</font>
 +
<br />
 +
::<source lang="text">
 +
ecx = 10
 +
ecx = 9
 +
ecx = 8
 +
ecx = 7
 +
ecx = 6
 +
ecx = 5
 +
ecx = 4
 +
ecx = 3
 +
ecx = 2
 +
ecx = 1
 +
</source>
 +
<br />
 +
==Looping. Version 3==
 +
<br />
 +
* Now try this program
 +
<br />
 +
::<source lang="asm">
 +
;;; ; 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
 +
</source>
 +
<br />
 +
* Read it carefully.  Can you predict what it will output?
 +
* Assemble, link, and run it.
 +
* <font color="magenta">Verify that your intuition was correct.</font>  If it was not, make sure you understand why.
 +
* You are now ready for the assignment.
 +
<br />
 +
 +
=Problem #1: warming up=
 +
<br />
 +
* 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. 
 +
<br />
 +
* Submit it to Moodle.
 +
 +
=Problem #2: adding a string=
 +
<br />
 +
* Write an assembly program called '''Hw3_2.asm''' that uses a loop to prints ecx in the following way:
 +
<br />
 +
::<source lang="text">
 +
1. ecx = 20
 +
2. ecx = 19
 +
3. ecx = 18
 +
4. ecx = 17
 +
...       
 +
20. ecx = 1
 +
</source>
 +
<br />
 +
:(I replaced a large number of lines using ellipses in the list above.)
 +
<br />
 +
 +
=Problem #3: adding an input=
 +
<br />
 +
* 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 a fixed number of times, i.e. 20 times, the program uses a number input by the user ''to control the loop''.
 +
* Example 1:
 +
<br />
 +
::<source lang="text">
 +
./Hw3_3
 +
> 4
 +
1. ecx = 4
 +
2. ecx = 3
 +
3. ecx = 2
 +
4. ecx = 1
 +
</source>
 +
<br />
 +
:Example 2:
 +
<br />
 +
::<source lang="text">
 +
./Hw3_3
 +
> 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
 +
</source>
 +
<br />
 +
: 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.
 +
* Submit your program on Moodle.
 +
<br />
 +
<tanbox>
 +
When you submit your program and evaluate it on Moodle, the number input by Moodle '''does not show in the output'''.  For example, the first test Moodle will try is to input 4 into your program.  The correct output, '''for Moodle''' will be:
 +
<br />
 +
<source lang="text">
 +
> 1. ecx = 4
 +
2. ecx = 3
 +
3. ecx = 2
 +
4. ecx = 1
 +
</source>
 +
<br />
 +
Do not try to emulate this output in your program.  The way your program should work is exactly as shown inExample 1 and Example 2 above, with 4 and 10.  Just be aware that when moodle runs your program, whatever it provides as an input does not show up in the program output.
 +
</tanbox>
 +
<br />
 +
 +
=Problem #4=
 +
<br />
 +
* Write a program called '''Hw3_4.asm''' that uses a loop to compute the first 40 Fibonacci numbers.
 +
* Your program must use at least one loop.
 +
* Your solution may use an array, but does not have to.  You are free to pick the implementation.  But you must use a loop to '''compute''' each Fibonacci as the sum of the previous two terms.  Of course you start with the first two terms equal to 1; no need to compute these.
 +
* The output of your program should be the following (no extra blank lines before or after):
 +
<br />
 +
::<source lang="text">
 +
1
 +
1
 +
2
 +
3
 +
5
 +
8
 +
13
 +
21
 +
34
 +
55
 +
89
 +
144
 +
233
 +
377
 +
610
 +
987
 +
1597
 +
2584
 +
4181
 +
6765
 +
10946
 +
17711
 +
28657
 +
46368
 +
75025
 +
121393
 +
196418
 +
317811
 +
514229
 +
832040
 +
1346269
 +
2178309
 +
3524578
 +
5702887
 +
9227465
 +
14930352
 +
24157817
 +
39088169
 +
63245986
 +
102334155
 +
</source>
 +
<br />
 +
* Submit your program on Moodle.
 +
<br />
 +
 +
=Problem #5=
 +
<br />
 +
* Write a program called '''Hw3_5.asm''' that works the exact same way as Problem #4, but displays a string in addition to the fibonacci terms.
 +
* Same restrictions as for Problem #4
 +
* The output of your program should be the following (no extra blank lines before or after):
 +
<br />
 +
::<source lang="text">
 +
Fibonacci(0) =1
 +
Fibonacci(1) =1
 +
Fibonacci(2) =2
 +
Fibonacci(3) =3
 +
Fibonacci(4) =5
 +
Fibonacci(5) =8
 +
Fibonacci(6) =13
 +
Fibonacci(7) =21
 +
Fibonacci(8) =34
 +
Fibonacci(9) =55
 +
Fibonacci(10) =89
 +
Fibonacci(11) =144
 +
Fibonacci(12) =233
 +
Fibonacci(13) =377
 +
Fibonacci(14) =610
 +
Fibonacci(15) =987
 +
Fibonacci(16) =1597
 +
Fibonacci(17) =2584
 +
Fibonacci(18) =4181
 +
Fibonacci(19) =6765
 +
Fibonacci(20) =10946
 +
Fibonacci(21) =17711
 +
Fibonacci(22) =28657
 +
Fibonacci(23) =46368
 +
Fibonacci(24) =75025
 +
Fibonacci(25) =121393
 +
Fibonacci(26) =196418
 +
Fibonacci(27) =317811
 +
Fibonacci(28) =514229
 +
Fibonacci(29) =832040
 +
Fibonacci(30) =1346269
 +
Fibonacci(31) =2178309
 +
Fibonacci(32) =3524578
 +
Fibonacci(33) =5702887
 +
Fibonacci(34) =9227465
 +
Fibonacci(35) =14930352
 +
Fibonacci(36) =24157817
 +
Fibonacci(37) =39088169
 +
Fibonacci(38) =63245986
 +
Fibonacci(39) =102334155
 +
</source>
 +
<br />
 +
* Submit your program on Moodle.
 +
<br />
 +
=Problem #6=
 +
<br />
 +
* Assume that you have a program that uses a loop to compute Fibonacci numbers.
 +
* The Fibonacci terms get stored in an array called Fib as they are computed.  The array is an array of '''dwords'''.
 +
* In the loop, the program computes
 +
<br />
 +
:::<source lang="java">
 +
          Fib[ i ] = Fib[ i-1 ] + Fib[ i-2 ];
 +
</source>
 +
<br />
 +
* If we are not worried about possible overflow of the arithmetic, i.e. we are not worried that the computation might become incorrect at some point, and if we assume that the array can be as large as we want (we have a lot of RAM in our computer), '''what is a good approximation to the number of Fibonacci terms an assembly program can compute in 1 second, if our computer has a 2.5 GHz Pentium processor?'''
 +
* Write your answer in '''Notepad''' or '''TextEdit''', and submit the text file on Moodle, in the '''Homework 3 Problem 6''' section.  Make sure you explain your answer fully.  Just putting down a number without justification, even is the number is in the acceptable range, will be worth the smallest possible grade over 0.  You may want to add a snippet of assembly code to support your explanations.
 +
<br />
 +
==Videos==
 +
<br />
 +
Quick video tutorials presenting how to quickly copy files from your Linux account to Moodle.
 +
<br />
 +
<center><videoflash>hGd0HD7tOZ4</videoflash></center>
 +
<br />
 +
<center><videoflash>Rq6-Epm09lA</videoflash></center>
 +
 +
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC231]][[Category:nasm]]

Latest revision as of 09:31, 2 October 2014

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



This assignment deals with loop that use the ecx register and the loop instruction, and with indirect, and based indexed addressing modes. This assignment is due Thursday, Oct. 9th, 2014, at 11:55 p.m. The first 3 loop sections are just for you to review loops, and do not need to be submitted.


Preparation


Go through the 3 different loop sections below to get ready for the homework problems for this week.

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 a fixed number of times, i.e. 20 times, the program uses a number input by the user to control the loop.
  • Example 1:


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


Example 2:


./Hw3_3
> 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.
  • Submit your program on Moodle.


When you submit your program and evaluate it on Moodle, the number input by Moodle does not show in the output. For example, the first test Moodle will try is to input 4 into your program. The correct output, for Moodle will be:

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


Do not try to emulate this output in your program. The way your program should work is exactly as shown inExample 1 and Example 2 above, with 4 and 10. Just be aware that when moodle runs your program, whatever it provides as an input does not show up in the program output.


Problem #4


  • Write a program called Hw3_4.asm that uses a loop to compute the first 40 Fibonacci numbers.
  • Your program must use at least one loop.
  • Your solution may use an array, but does not have to. You are free to pick the implementation. But you must use a loop to compute each Fibonacci as the sum of the previous two terms. Of course you start with the first two terms equal to 1; no need to compute these.
  • The output of your program should be the following (no extra blank lines before or after):


1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155


  • Submit your program on Moodle.


Problem #5


  • Write a program called Hw3_5.asm that works the exact same way as Problem #4, but displays a string in addition to the fibonacci terms.
  • Same restrictions as for Problem #4
  • The output of your program should be the following (no extra blank lines before or after):


Fibonacci(0) =1
Fibonacci(1) =1
Fibonacci(2) =2
Fibonacci(3) =3
Fibonacci(4) =5
Fibonacci(5) =8
Fibonacci(6) =13
Fibonacci(7) =21
Fibonacci(8) =34
Fibonacci(9) =55
Fibonacci(10) =89
Fibonacci(11) =144
Fibonacci(12) =233
Fibonacci(13) =377
Fibonacci(14) =610
Fibonacci(15) =987
Fibonacci(16) =1597
Fibonacci(17) =2584
Fibonacci(18) =4181
Fibonacci(19) =6765
Fibonacci(20) =10946
Fibonacci(21) =17711
Fibonacci(22) =28657
Fibonacci(23) =46368
Fibonacci(24) =75025
Fibonacci(25) =121393
Fibonacci(26) =196418
Fibonacci(27) =317811
Fibonacci(28) =514229
Fibonacci(29) =832040
Fibonacci(30) =1346269
Fibonacci(31) =2178309
Fibonacci(32) =3524578
Fibonacci(33) =5702887
Fibonacci(34) =9227465
Fibonacci(35) =14930352
Fibonacci(36) =24157817
Fibonacci(37) =39088169
Fibonacci(38) =63245986
Fibonacci(39) =102334155


  • Submit your program on Moodle.


Problem #6


  • Assume that you have a program that uses a loop to compute Fibonacci numbers.
  • The Fibonacci terms get stored in an array called Fib as they are computed. The array is an array of dwords.
  • In the loop, the program computes


           Fib[ i ] = Fib[ i-1 ] + Fib[ i-2 ];


  • If we are not worried about possible overflow of the arithmetic, i.e. we are not worried that the computation might become incorrect at some point, and if we assume that the array can be as large as we want (we have a lot of RAM in our computer), what is a good approximation to the number of Fibonacci terms an assembly program can compute in 1 second, if our computer has a 2.5 GHz Pentium processor?
  • Write your answer in Notepad or TextEdit, and submit the text file on Moodle, in the Homework 3 Problem 6 section. Make sure you explain your answer fully. Just putting down a number without justification, even is the number is in the acceptable range, will be worth the smallest possible grade over 0. You may want to add a snippet of assembly code to support your explanations.


Videos


Quick video tutorials presenting how to quickly copy files from your Linux account to Moodle.