CS Computer Organization and Assembly Language Programming Final Exam

CS 350 - Computer Organization and Assembly Language Programming Final Exam Each Question Carries 25 Points – Partial solution is given to Problem 2 t...
Author: Rolf Henry
30 downloads 1 Views 83KB Size
CS 350 - Computer Organization and Assembly Language Programming Final Exam Each Question Carries 25 Points – Partial solution is given to Problem 2 to reduce the complexity. Problem 1: Write a procedure that receives a number in AX, counts the number of ones in its binary representation, and returns this count in BX. Have it do this using no more than seven instructions. Problem 2: Write a procedure that does not use CMP nor SUB, but instead uses one logic instruction to test whether or not AX and BX contain the same word. If so, have it return 1 in AX, else have it return 0 in AX. Do all of this using no more than six instructions. Use the given procedure for part of the solution. MoveZeros2 PROC : This is an alteration of BubbleSort push AX push DX mov DX, CX dec CX L1: push CX push SI L2: mov AX, [SI] cmp AX, 0 jne L3 xchg AX, [SI+2] ; Swap when and only when first of two is zero. mov [SI], AX L3: inc SI inc SI loop L2 mov AX, [SI] cmp AX, 0 jne L4 dec DX ; Decrement DX whenever a zero just got pushed to the end. L4: pop SI pop CX loop L1 mov CX, DX pop DX pop AX ret MoveZeros2 ENDP MoveZeros3 PROC ; This method uses the stack. push AX push BX push DX push SI mov DX, CX mov BX, 0 add SI, CX add SI, CX sub SI, 2 Start at end of array and push. CS 350 – Final Exam – Page 1

L1:

push [SI] sub SI, 2 loop L1 add SI, 2 mov CX, DX L2: pop AX ; Pop nonzero entries back into array. cmp AX, 0 je L3 mov [SI] , AX add SI, 2 loop L2 jmp L4 L3: inc BX ; Count # zero entries. loop L2 L4: mov CX, BX cmp CX, 0 je L6 L5: mov AX, 0 ; Put needed zeros at the end of array. mov [SI], AX add SI, 2 loop L5 L6: mov CX, DX sub CX, BX pop SI pop DX pop BX pop AX MoveZeros3 ENDP END main Problem 3: Convert the following C code to equivalent MASM code: if(a0); if( a == 0 && b== 0 ) c++; } Problem 4: Pretend that the l6-bit Pentium registers are only 8-bit registers. By means of a table, show how the registers change by showing what things look like upon each arrival at line L2, assuming that initially SI=25 (decimal) and DI=13 (decimal). Express the contents of the registers in binary here. SI = 000110M (fixed). CS 350 – Final Exam – Page 2

CS 350 - Computer Organization and Assembly Language Programming Final Exam Solutions Problem 1: Write a procedure that receives a number in AX, counts the number of ones in its binary representation, and returns this count in BX. Have it do this using no more than seven instructions. Solution: CountOnes PROC mov BX, 0 mov CX, 16 L: sh1 AX, 1 jnc S inc BX S: loop L ret CountOnes ENDP Problem 2: Write a procedure that does not use CMP nor SUB, but instead uses one logic instruction to test whether or not AX and BX contain the same word. If so, have it return 1 in AX, else have it return 0 in AX. Do all of this using no more than six instructions. Solution: Compare PROC xor AX, BX jz s mov AX, O ret S: mov AX, 1 ret Compare ENDP mov CX, N call MoveZeros3 ; test third solution ; mov CX, N L3: mov AX, [SI] call WriteDec mov AL, '' call WriteChar inc SI inc SI loop L3 call CrLf call CrLf exit main ENDP MoveZeros1 PROC push AX CS 350 – Final Exam – Page 3

push DX push SI mov DX, CX L1: mov AX, [SI] cmp AX, 0 je L2 inc SI inc SI loop L1 jmp L4 L2: dec DX cmp CX,1 je done dec CX push CX push SI L3: mov AX, [sr+2] mov [SI] ,AX inc SI inc SI loop L3 mov AX, 0 mov [SI] ,AX pop SI pop CX inc CX loop L1 L4: mov CX, DX pop SI pop DX pop AX ret MoveZeros1 ENDP MoveZeros2 PROC : This is an alteration of BubbleSort push AX push DX mov DX, CX dec CX L1: push CX push SI L2: mov AX, [SI] cmp AX, 0 jne L3 xchg AX, [SI+2] ; Swap when and only when first of two is zero. mov [SI], AX L3: inc SI inc SI loop L2 CS 350 – Final Exam – Page 4

mov AX, [SI] cmp AX, 0 jne L4 dec DX ; Decrement DX whenever a zero just got pushed to the end. L4: pop SI pop CX loop L1 mov CX, DX pop DX pop AX ret MoveZeros2 ENDP MoveZeros3 PROC ; This method uses the stack. push AX push BX push DX push SI mov DX, CX mov BX, 0 add SI, CX add SI, CX sub SI, 2 Start at end of array and push. L1: push [SI] sub SI, 2 loop L1 add SI, 2 mov CX, DX L2: pop AX ; Pop nonzero entries back into array. cmp AX, 0 je L3 mov [SI] , AX add SI, 2 loop L2 jmp L4 L3: inc BX ; Count # zero entries. loop L2 L4: mov CX, BX cmp CX, 0 je L6 L5: mov AX, 0 ; Put needed zeros at the end of array. mov [SI], AX add SI, 2 loop L5 L6: mov CX, DX sub CX, BX pop SI pop DX pop BX CS 350 – Final Exam – Page 5

pop AX MoveZeros3 ENDP END main Problem 3: Convert the following C code to equivalent MASM code: if(a0); if( a == 0 && b== 0 ) c++; } Solution: mov AX, b add AX, c cmp a, AX jge done L1: mov AX, b cmp AX, c je L2 cmp a, AX jge L3 L2: mov AX, a add AX, b mov a, AX L3: dec c mov AX, c cmp AX, 0 jg L1 mov AX, a cmp AX, 0 jne done mov AX, b cmp AX, 0 jne done inc c done: ......

CS 350 – Final Exam – Page 6

Problem 4: Pretend that the l6-bit Pentium registers are only 8-bit registers. By means of a table, show how the registers change by showing what things look like upon each arrival at line L2, assuming that initially SI=25 (decimal) and DI=13 (decimal). Express the contents of the registers in binary here. Solution: S1 = 000110M (fixed) MYMUL PROC push DI push CX mov AX, 0 mov DX, 0 mov CX, 8 L1: shl AX, 1 rcl DX, 1 shl DI, 1 jnc L2 add AX, SI jnc L2 inc DX L2: loop L1 pop CX pop DI ret MYMUL ENDP

DI 00001101 00011010 00110100 01101000 11010000 10100000 01000000 10000000 00000000

DX 00000000 00000001

CS 350 – Final Exam – Page 7

AX 00000000 00011001 00110010 01001011 10010110 00101100 01000101

Suggest Documents