Difference between revisions of "CSC231 Homework 2 2014"

From dftwiki3
Jump to: navigation, search
(Problem #2)
(Problem #1)
 
(22 intermediate revisions by the same user not shown)
Line 103: Line 103:
 
    
 
    
 
<br />
 
<br />
 
+
:where both ''x'' and ''z'' are 32-bit dwords (defined by '''dd''' directives in the program).
 +
<br />
 
<tanbox>
 
<tanbox>
'''Imporant Note''': do not use multiply and divide yet.  They are complex instructions to master, and beyond our scope at this point.  We'll see them later.  So, when you have to multiply quatities, figure out how to use '''add''' or '''sub''' instead, and generate the same result.
+
'''Imporant Note''': do not use multiply or divide instructions yet.  They are complex instructions to master, and beyond our scope at this point.  We'll see them later.  So, when you have to multiply quatities, figure out how to use '''add''' or '''sub''' instead, and generate the same result.
 
</tanbox>
 
</tanbox>
 
<br />
 
<br />
Line 120: Line 121:
 
  z = 399
 
  z = 399
 
   
 
   
==Moodle Submission==
+
 
<br />
+
* Locate the area for Homework 2, Problem 1 section in Moodle, and submit your program there.
* Locate the area for Homework 2, Problem 1, and submit your program there.
 
 
<br />
 
<br />
  
Line 131: Line 131:
 
  z = 2 * x - 3 * y + 7
 
  z = 2 * x - 3 * y + 7
  
 +
: where x, y, and z are still 32-bit dwords.
 +
<br />
 
* Example of output:
 
* Example of output:
 
   
 
   
Line 148: Line 150:
 
   
 
   
 
<br />
 
<br />
:<tanbox>'''Note''': the function '''_printDec''' does not know how to print negative numbers.  We'll see why later.  So if your output looks like a large positive number while it should be negative, that's normal!
+
<tanbox>'''Note''': the function '''_printDec''' does not know how to print negative numbers.  We'll see why later.  So if your output looks like a large positive number while it should be negative, that's normal!
 
</tanbox>
 
</tanbox>
 
<br />
 
<br />
==Moodle Submission==
+
* Locate the area for Homework 2, Problem 2 section in Moodle, and submit your program there.
 +
<br />
 +
 
 +
=Problem #3=
 +
<br />
 +
* This time the program is called '''Hw2_3.asm''', gets 2 integers ''x'' and ''y'' from the user, and computes the equation:
 +
<br />
 +
 +
z = ( x + y )* 3 - ( x - y ) * 4
 +
 +
<br />
 +
: where x, y, and z are 32-bit dwords.
 +
<br />
 +
* Typical output:
 +
<br />
 +
 +
./hw2_3
 +
> 3
 +
> 1
 +
x = 3
 +
y = 1
 +
z = 4
 +
 +
./hw2_3
 +
> 4
 +
> 2
 +
x = 4
 +
y = 2
 +
z = 10
 +
 +
<br />
 +
* Locate the area for Homework 2, Problem 3 section in Moodle, and submit your program there.
 +
<br />
 +
<br />
 +
<bluebox>
 +
The next problems are repeats of the irst 3 problems, but the variables change size.
 +
</bluebox>
 +
<br />
 +
 
 +
=problem #4=
 +
<br />
 +
* Same as problem #1, but this time, ''x'' and ''z'' are 16-bit words.  And you must do all operations on 16-bit registers or memory.  ''x'' and ''z'' are declared as follows:
 +
<br />
 +
<source lang="asm">
 +
 
 +
x        dw        0
 +
z        dw        0
 +
</source>
 +
<br />
 +
 +
* To get the value of ''x'', your code must contained these two lines:
 +
 
 +
<br />
 +
<source lang="asm">
 +
    call  _getInput            ;get user input in eax
 +
    mov  word[x], ax            ;put lower 16-bit part of eax in x
 +
</source>
 +
<br />
 +
 
 +
* The problem for you, then, is that when you have finished computing ''z'', you have a 16-bit value in ''z'', but the  '''_printDec''' function expect what it needs to print in eax, which is a 32-bit register.  So you have to put ''z'' in the lower part of eax (i.e. ax), and make sure the upper part of eax contains 0.  Try to figure out how to make this happen.
 +
 
 +
* Note: You '''cannot''' modify 231Lib.asm.  You have to use the original ''_getInput'' and ''_printDec'' functions.  You cannot modify them!  You ''cannot'' add new functions for outputting or inputting data.
 +
* You ''may'' use additional variables, but you shouldn't have to.
 +
<br />
 +
* Locate the area for Homework 2, Problem 4 section in Moodle, and submit your program there.
 +
<br />
 +
<br />
 +
 
 +
=problem #5=
 +
<br />
 +
* Same problem as Problem #2, but this time, ''x'', ''y'', and ''z'' are 16-bit words, defined as follows:
 +
<br />
 +
<source lang="asm">
 +
x      dw        0
 +
y      dw        0
 +
z      dw        0
 +
</source>
 +
<br />
 +
* Same comments as for Problem 4.
 +
<br />
 +
* Locate the area for Homework 2, Problem 5 section in Moodle, and submit your program there.
 +
<br />
 +
 
 +
=Problem #6=
 +
<br />
 +
* Same as problem #3, but this time ''x'', ''y'', and ''z'' are 8-bit bytes.
 +
<br />
 +
* Locate the area for Homework 2, Problem 6 section in Moodle, and submit your program there.
 +
<br />
 +
 
 +
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 
<br />
 
<br />
* Submit your program to the Homework 2, Problem 2 section on Moodle.
 
 
<br />
 
<br />
 +
[[Category:CSC231]][[Category:nasm]]

Latest revision as of 17:01, 30 September 2014

--D. Thiebaut (talk) 09:48, 21 September 2014 (EDT)


This homework is due on Tuesday, Sept. 30th, at 11:55 p.m.


Preparation


  • Create a program called IOskel.asm using your 231a-xx account, on a Linux machine (beowulf2, grendel, or any of the Linux Mint PCs in FH342 or FH345, and copy this code in it:


;;; -----------------------------------------------------
;;; IOskel.asm
;;; D. Thiebaut
;;; Simple skeleton program illustrating how to get an
;;; integer from the command line and displaying it back
;;;
;;; To assemble, link and run:
;;; nasm -f elf 231Lib.asm
;;; nasm -f elf IOskel.asm
;;; ld -melf_i386 IOskel.o 231Lib.o
;;; ./IOskel
;;; -----------------------------------------------------

;;; extern functions that will be linked to this program
;;; contained in 231Lib.asm

extern  _printDec
extern  _printString
extern  _println
extern  _getInput
        
;;; -----------------------------------------------------
;;; data section
;;; -----------------------------------------------------
        section .data
x       dd      0               ; the integer used for IO
msgX    db      "x = "
prompt  db      "> "
        
;;; -----------------------------------------------------
;;; code section
;;; -----------------------------------------------------
        section .text
        global _start
_start:

;;; get number from user
        mov             ecx, prompt
        mov             edx, 2
        call            _printString
        call            _getInput
        mov             dword[x], eax

        
;;; print "x = dddd" where dddd is the contents of x
;;; in decimal
        mov             ecx, msgX
        mov             edx, 4
        call            _printString
        mov             eax, dword[x]
        call            _printDec
        call            _println

;;; ; exit
        mov     ebx, 0
        mov     eax, 1
        int     0x80


  • Also create another program called 231Lib.asm, and copy the code found on this page in it.
  • 231Lib.asm is a library of functions that will be useful for this assignment. You do not, and should not have to modify the code in this program.
  • IOskel.asm is a skeleton which you will have to copy into new assembly programs, which you will edit to solve various problems.
  • Assemble, link, and run the programs as follows (user input in boldface):


nasm -f elf IOskel.asm
nasm -f elf 231Lib.asm       (once you have assembled it, you don't need to repeat this step)
ld -melf_i386 -o IOskel IOskel.o 231Lib.o
./IOskel
>
 


  • The ">" symbol is a prompt: the program is asking you to input a number. Just input an integer, and press Enter:


> 22334
x = 22334


Problem #1


  • Copy IOskel.asm into a new program, called hw2_1.asm


cp IOskel.asm hw2_1.asm


  • Add new code between the 'call _println and the ;;; exit lines. This code should compute and display the following arithmetic operation:
z = x * 4 - 1;
 


where both x and z are 32-bit dwords (defined by dd directives in the program).


Imporant Note: do not use multiply or divide instructions yet. They are complex instructions to master, and beyond our scope at this point. We'll see them later. So, when you have to multiply quatities, figure out how to use add or sub instead, and generate the same result.


  • Here are two examples of the output expected:
./hw2_1 
> 344
x = 344
z = 1375

./hw2_1 
> 100
x = 100
z = 399

  • Locate the area for Homework 2, Problem 1 section in Moodle, and submit your program there.


Problem #2


  • This is similar to what you had to do for Problem 1: Create a new program called Hw2_2.asm which gets 2 integers (which your program will call x, and y) from the user and computes this equation:
z = 2 * x - 3 * y + 7
where x, y, and z are still 32-bit dwords.


  • Example of output:
./hw2_2
> 10
> 1
x = 10
y = 1
z = 24
 
./hw2_2
> 20
> 2
x = 20
y = 2
z = 41


Note: the function _printDec does not know how to print negative numbers. We'll see why later. So if your output looks like a large positive number while it should be negative, that's normal!


  • Locate the area for Homework 2, Problem 2 section in Moodle, and submit your program there.


Problem #3


  • This time the program is called Hw2_3.asm, gets 2 integers x and y from the user, and computes the equation:


z = ( x + y )* 3 - ( x - y ) * 4


where x, y, and z are 32-bit dwords.


  • Typical output:


./hw2_3
> 3
> 1
x = 3
y = 1
z = 4

./hw2_3
> 4
> 2
x = 4
y = 2
z = 10


  • Locate the area for Homework 2, Problem 3 section in Moodle, and submit your program there.



The next problems are repeats of the irst 3 problems, but the variables change size.


problem #4


  • Same as problem #1, but this time, x and z are 16-bit words. And you must do all operations on 16-bit registers or memory. x and z are declared as follows:


 x         dw         0
 z         dw         0


  • To get the value of x, your code must contained these two lines:


    call   _getInput            ;get user input in eax
    mov  word[x], ax            ;put lower 16-bit part of eax in x


  • The problem for you, then, is that when you have finished computing z, you have a 16-bit value in z, but the _printDec function expect what it needs to print in eax, which is a 32-bit register. So you have to put z in the lower part of eax (i.e. ax), and make sure the upper part of eax contains 0. Try to figure out how to make this happen.
  • Note: You cannot modify 231Lib.asm. You have to use the original _getInput and _printDec functions. You cannot modify them! You cannot add new functions for outputting or inputting data.
  • You may use additional variables, but you shouldn't have to.


  • Locate the area for Homework 2, Problem 4 section in Moodle, and submit your program there.



problem #5


  • Same problem as Problem #2, but this time, x, y, and z are 16-bit words, defined as follows:


 x       dw        0
 y       dw        0
 z       dw        0


  • Same comments as for Problem 4.


  • Locate the area for Homework 2, Problem 5 section in Moodle, and submit your program there.


Problem #6


  • Same as problem #3, but this time x, y, and z are 8-bit bytes.


  • Locate the area for Homework 2, Problem 6 section in Moodle, and submit your program there.