CSC231 Final Exam 2010

From dftwiki3
Revision as of 15:13, 16 December 2010 by Thiebaut (talk | contribs) (Part 3 (0.5 point))
Jump to: navigation, search

This final exam is take-home. It is open-books, open-notes, and open-Web. It is due a week after it is made available, at 12:00 p.m. on Monday December 20, 2010.

You cannot discuss the details of this exam with anyone except your instructor. The TAs are not allowed to help you out in any way. No question will be answered in person after 12:00 a.m. on 12/13/10. Instead, if you have questions regarding the exam, send them via email to thiebaut@cs.smith.edu, and the question and its answer will be broadcast back to the hole class via email. The exam is given under the rules of the Smith College Honor Code.

Make sure you reference all work/resources you use in your documentation.

Files submitted past the deadline will not be graded.










Problem #1: Recursive GCD (1 point)

The following algorithm can be used to find the greatest common denominator of two integers, or GCD. The GCD of two integers m and n is the largest integer that divides both of them with a remainder of 0. The GCD of 5 and 6 is 1. The GCD of 10 and 12 is 2. The GCD of 12 and 20 is 4.

int gcd(int m, int n) {
   if ((m % n) == 0)     
      return n;
   else
      return gcd(n, m % n);
}

The % sign is the modulo operator.

Implement this algorithm in assembly using a recursive function called gcd. Make it compute and output the gcd of (5, 6), (10, 12), and (20, 30).

Call your program final1.asm and submit it as follows:

 submit final final1.asm


Problem #2: Debugging Utility Functions

Part 1 (1.5 points)

Write an assembly language program that does not use the driver.c or asm_io.asm files and that displays a 32-bit integer in binary, hexadecimal, and in decimal. When the number is displayed in decimal it is displayed as an unsigned int. For example, 0x80000000 should display as 2147483648.

Your program should contain three functions, one that receives the 32-bit integer in eax and displays it in binary. One that receives the 32-bit integer in eax and displays in hexadecimal. The third one will receive the 32-bit integer in eax and display it in decimal, without leading zeros.

Demonstrate the functionality of your program by making it display the following 32-bit integers:


1, 
0xF, 
0X10
0x12345678 
0x7ffffff 
0x89abcdef 
0xffffffff

Here is what your main program should look like:

	section	.data
table	dd	1, 15, 16
        dd      0x12345678, 0x7ffffff, 0x89abcdef, 0xffffffff,
N       equ     ($-table)/4

        section .text
start:
	mov	ebx, table
	mov	ecx, N
for:
        mov     eax, [ebx]

        call    displayBin
        call    nextLine
	call	displayHex
        call    nextLine
	call    displayUInt
        call    nextLine
        call    nextLine
	
        add     ebx, 4
	loop	for

	mov	eax, EXIT
	mov	ebx, 0
	int	0x80
        

where nextLine is a function that brings the cursor to the next line.

Part 2 (1 point)

Add a fourth function called printRegs to your program that will display all the registers in hex, in unsigned decimal, and in signed decimal formats.

Here is an example of the call and its output:

call	printRegs
eax: 00000000 0	      	 0
ebx: 00001000 4096    	 4096
ecx: 0fabbcde 262913246	 262913246
edx: 0000fffe 65534   	 65534
esi: 00001132 4402    	 4402
edi: ffffffff 4294967295 -1
ebp: 0ffd1100 268243200  268243200
esp: 0ffd1100 268243200	 268243200

Your function should display the value the registers hold before the call is made to printRegs. Your function should return to the calling program without having changed the value of any of the registers.

Part 3 (0.5 point)

Add a new feature to the printRegs function so that it displays the flag bits.

The flag bits are located at specific positions in the flags register: The EFLAGS register

Taken from http://www.eecg.toronto.edu/~amza/www.mindsec.com/files/x86regs.html: The EFLAGS register hold the state of the processor. It is modified by many intructions and is used for comparing some parameters, conditional loops and conditionnal jumps. Each bit holds the state of specific parameter of the last instruction. Here is a listing :

Bit   Label    Desciption
---------------------------
0      CF      Carry flag
2      PF      Parity flag
4      AF      Auxiliary carry flag
6      ZF      Zero flag
7      SF      Sign flag
8      TF      Trap flag
9      IF      Interrupt enable flag
10     DF      Direction flag
11     OF      Overflow flag
12-13  IOPL    I/O Priviledge level
14     NT      Nested task flag
16     RF      Resume flag
17     VM      Virtual 8086 mode flag
18     AC      Alignment check flag (486+)
19     VIF     Virutal interrupt flag
20     VIP     Virtual interrupt pending flag
21     ID      ID flag

Those that are not listed are reserved by Intel.

You need to display only the Carry, Parity, Zero, and Sign bits.

Make sure that your function displays the status of the bits as they stand before the function is called, and make sure that whatever your function does, when it returns to the caller the status bits are in the same state they were before the function was called.

You will need the pushf and (maybe) the popf instructions. Pushf pushes a doubleword containing the flags register in the stack. Popf pops it back.

Requirements

  • Submit only code that works
  • Document your code well:
    • Functions should have headers
    • The main program should have a header.

Submission

Submit only one program for all parts. Call it final2.asm, and submit it as follows:

submit final final2.asm