Difference between revisions of "CSC231 Homework 7 2014"

From dftwiki3
Jump to: navigation, search
(Problem #1)
(Problem #4)
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 10:33, 11 November 2014 (EST)
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 10:33, 11 November 2014 (EST)
 
----
 
----
<center>
+
<font size="+2">Page under construction</font>
 
</center>
 
<br />
 
<center>
 
[[File:UnderConstruction.jpg|300px]]
 
</center>
 
 
<br />
 
<br />
 
<bluebox>This assignment is due on 11/18/14, at 11:55 p.m.
 
<bluebox>This assignment is due on 11/18/14, at 11:55 p.m.
Line 20: Line 14:
 
=Problem #1=
 
=Problem #1=
 
<br />
 
<br />
* Use the 231Lib.asm library as inspiration and write a function called '''printInt''' that prints a 32-bit 2-s complement number on the screen.   
+
* Use the 231Lib.asm library as inspiration and write a function called '''_printInt''' that prints a 32-bit 2-s complement number in decimal on the screen.   
* Your function should work similarly to '''printDec''', in that the number to print is '''passed through eax''', and not through the stack!
+
* Your function should work similarly to '''_printDec''', in that the number to print is '''passed through eax''', and not through the stack!   For example, if we store 0xFFFFFFFF in eax and call your function, it should print -1 on the screen.
 
* Your function should '''not modify any of the registers''', including eax.
 
* Your function should '''not modify any of the registers''', including eax.
* Your function should be made '''global''' so that a test program can call it.
+
* Your function should be made '''global''' in your program file, so that the function can be called by a test program.
 
* Your function can call _printDec to do some of its printing.
 
* Your function can call _printDec to do some of its printing.
* Submit your function in a program called '''Hw7_1.asm'''.  Make sure that your Hw7_1.asm program does not contain a _start label, otherwise the linker will be confused when it finds two global labels with the same name in your program and in the test program.
+
* Submit your function in a program called '''Hw7_1.asm''' to Moodle.  Make sure that your Hw7_1.asm program does not contain a _start label, otherwise the linker will be confused when it finds two global labels with the same name, in your program, and in the test program.
 
<br />
 
<br />
 
==Example Test Program==
 
==Example Test Program==
Line 32: Line 26:
 
           section .text
 
           section .text
  
           extern  printInt
+
           extern  _printInt
 
_start:
 
_start:
 
           mov    eax, 0
 
           mov    eax, 0
           call    printInt         ; prints 0
+
           call    _printInt         ; prints 0
 
    
 
    
 
           mov    eax, 0xff
 
           mov    eax, 0xff
           call    printInt         ; prints 255
+
           call    _printInt         ; prints 255
  
 
           mov    eax, -1
 
           mov    eax, -1
           call    printInt         ; prints -1
+
           call    _printInt         ; prints -1
  
 
           mov    eax, 0xffffffff
 
           mov    eax, 0xffffffff
           call    printInt         ; prints -1
+
           call    _printInt         ; prints -1
 
            
 
            
 
           mov    eax, 1
 
           mov    eax, 1
Line 55: Line 49:
 
=Problem #2=
 
=Problem #2=
 
<br />
 
<br />
* Write a function called '''printInt16''' that prints a 16-bit 2's complement number on the screen.   
+
* Write a function called '''_printInt16''' that prints a 16-bit 2's complement number on the screen.   
* Your function should work similarly to '''printInt''', but the number it prints is the number passed in '''ax''' (not eax).
+
* Your function should work similarly to '''_printInt''', but the number it prints is the number passed in '''ax''' (not eax).
 
* Your function should '''not modify any of the registers''', including ax or the upper part of eax.
 
* Your function should '''not modify any of the registers''', including ax or the upper part of eax.
 
* Your function should be made '''global''' so that a test program can call it.
 
* Your function should be made '''global''' so that a test program can call it.
 +
* Your function can call your _printInt function (see previous problem) to do some of its printing.
 
* Submit your function to Moodle in a program called '''Hw7_2.asm'''.  Also, as with Program 1, make sure that your Hw7_2.asm program does not contain a _start label.
 
* Submit your function to Moodle in a program called '''Hw7_2.asm'''.  Also, as with Program 1, make sure that your Hw7_2.asm program does not contain a _start label.
 
<br />
 
<br />
Line 69: Line 64:
 
_start:
 
_start:
 
           mov    ax, 0
 
           mov    ax, 0
           call    printInt16         ; prints 0
+
           call    _printInt16         ; prints 0
 
    
 
    
 
           mov    ax, 0xff
 
           mov    ax, 0xff
           call    printInt16         ; prints 255
+
           call    _printInt16         ; prints 255
  
 
           mov    eax, 0x0000ffff
 
           mov    eax, 0x0000ffff
           call    printInt16         ; prints -1
+
           call    _printInt16         ; prints -1
  
 
           mov    eax, 0xffff0001
 
           mov    eax, 0xffff0001
           call    printInt16         ; prints 1
+
           call    _printInt16         ; prints 1
 
            
 
            
 
           mov    eax, 1
 
           mov    eax, 1
Line 85: Line 80:
 
</source>
 
</source>
 
<br />
 
<br />
 +
 
=Problem #3=
 
=Problem #3=
 
<br />
 
<br />
Please answer the multiple-choice questions in Moodle, Section Homework 7 Problem 3.
+
Please answer the multiple-choice questions in Moodle, Section Homework 7 Problem 3.   Only one trial is allowed for each question.
 
<br />
 
<br />
 +
 
=Problem #4=
 
=Problem #4=
 
<br />
 
<br />
Line 96: Line 93:
 
<br />
 
<br />
  
<source lang="cpp">
+
::<source lang="cpp">
 
#include <iostream>
 
#include <iostream>
  
 
using namespace std;
 
using namespace std;
  
main() {
+
int main() {
 
   unsigned int x, lastx;
 
   unsigned int x, lastx;
  
Line 123: Line 120:
 
   }
 
   }
 
   cout << endl << endl << endl << endl;
 
   cout << endl << endl << endl << endl;
 +
  return 0;
 
}
 
}
 
</source>
 
</source>
Line 135: Line 133:
 
   ./hw7
 
   ./hw7
  
The following questions should be answered in Moodle, Section Homework 7, Problem 4.
+
The following questions should be answered in Moodle, Section Homework 7, Problem 4. Only one trial is allowed for each question.
 +
<br />
 
;Question 1
 
;Question 1
 
: Why does the loop stop?
 
: Why does the loop stop?
 +
<br />
  
 
;Question 2
 
;Question 2
: How many bits does C++ use to store integers (the variable x)?
+
: From your observation the output, how many bits does C++ use to store integers (the variable x)?
 +
<br />
  
 
;Question 3
 
;Question 3
: Remove the word ''unsigned'' in front of ''int'', compile and  run the program again.   Why does the loop stop?
+
: Remove the word ''unsigned'' in front of ''int'', compile and  run the program again.   From your observation the output, how many bits do you know C++ use for regular (not unsigned) ints?
 +
<br />
  
 
;Question 4
 
;Question 4
: Change the words "unsigned int" and replace them by "char". Compile and run the program again.  How many bits are used by C++ to store characters? 
+
: Replace "int" by "char" in the declaration of x and lastx.   Change the way x and lastx are printed by typecasting them to int first:
 +
::<source lang="cpp">
 +
  cout << "x = " << (int) x << endl;
 +
  ...
 +
  cout << "last x = " << (int) lastx << endl;
 +
  cout << "x = " << (int) x << endl;
 +
</source>
 +
<br />
  
;Question 5
+
: Compile and run the program againFrom your observation the output, how many bits are used by C++ to store characters?
: Same setup as Question 4Are characters treated as signed or unsigned in C++?
 
  
 +
 
<br />
 
<br />
 
<br />
 
<br />

Latest revision as of 21:41, 15 November 2014

--D. Thiebaut (talk) 10:33, 11 November 2014 (EST)



This assignment is due on 11/18/14, at 11:55 p.m.


Preparation


  • You may want to do some reading about the NEG and NOT instructions

Problem #1


  • Use the 231Lib.asm library as inspiration and write a function called _printInt that prints a 32-bit 2-s complement number in decimal on the screen.
  • Your function should work similarly to _printDec, in that the number to print is passed through eax, and not through the stack! For example, if we store 0xFFFFFFFF in eax and call your function, it should print -1 on the screen.
  • Your function should not modify any of the registers, including eax.
  • Your function should be made global in your program file, so that the function can be called by a test program.
  • Your function can call _printDec to do some of its printing.
  • Submit your function in a program called Hw7_1.asm to Moodle. Make sure that your Hw7_1.asm program does not contain a _start label, otherwise the linker will be confused when it finds two global labels with the same name, in your program, and in the test program.


Example Test Program


          section .text

          extern  _printInt
_start:
          mov     eax, 0
          call    _printInt         ; prints 0
  
          mov     eax, 0xff
          call    _printInt         ; prints 255

          mov     eax, -1
          call    _printInt         ; prints -1

          mov     eax, 0xffffffff
          call    _printInt         ; prints -1
          
          mov     eax, 1
          mov     ebx, 0
          int     0x80


Problem #2


  • Write a function called _printInt16 that prints a 16-bit 2's complement number on the screen.
  • Your function should work similarly to _printInt, but the number it prints is the number passed in ax (not eax).
  • Your function should not modify any of the registers, including ax or the upper part of eax.
  • Your function should be made global so that a test program can call it.
  • Your function can call your _printInt function (see previous problem) to do some of its printing.
  • Submit your function to Moodle in a program called Hw7_2.asm. Also, as with Program 1, make sure that your Hw7_2.asm program does not contain a _start label.


Example Test Program


          section .text

          extern  printInt16
_start:
          mov     ax, 0
          call    _printInt16         ; prints 0
  
          mov     ax, 0xff
          call    _printInt16         ; prints 255

          mov     eax, 0x0000ffff
          call    _printInt16         ; prints -1

          mov     eax, 0xffff0001
          call    _printInt16         ; prints 1
          
          mov     eax, 1
          mov     ebx, 0
          int     0x80


Problem #3


Please answer the multiple-choice questions in Moodle, Section Homework 7 Problem 3. Only one trial is allowed for each question.

Problem #4


  • Use emacs to create a file that you will call hw7.cpp
  • Store the following C++ program in this file:


#include <iostream>

using namespace std;

int main() {
  unsigned int x, lastx;

  // print 4 blank lines
  cout << endl << endl << endl << endl;

  // initialize x and show its first value
  x = 3;
  cout << "x = " << x << endl;

  // loop for-ever... 
  while ( 1 ) { 
    lastx = x;
    x = x * 2;
    // if the value of x is ever less than or equal to lastx, stop the loop
    if ( x <= lastx ) {
      cout << "last x = " << lastx << endl;
      cout << "x = " << x << endl;
      break;
    }
  }
  cout << endl << endl << endl << endl;
  return 0;
}


  • Compile the program as follows:
 g++ hw7.cpp -o hw7
  • Run the program as follows:
 ./hw7

The following questions should be answered in Moodle, Section Homework 7, Problem 4. Only one trial is allowed for each question.

Question 1
Why does the loop stop?


Question 2
From your observation the output, how many bits does C++ use to store integers (the variable x)?


Question 3
Remove the word unsigned in front of int, compile and run the program again. From your observation the output, how many bits do you know C++ use for regular (not unsigned) ints?


Question 4
Replace "int" by "char" in the declaration of x and lastx. Change the way x and lastx are printed by typecasting them to int first:
   cout << "x = " << (int) x << endl;
   ...
   cout << "last x = " << (int) lastx << endl;
   cout << "x = " << (int) x << endl;


Compile and run the program again. From your observation the output, how many bits are used by C++ to store characters?