ECE291 Spring 2002 Exam 1

Solutions

Section Your Score Max
1. Fundamentals 30  RC 30 points
2. Addressing Memory 40  JQ 40 points
3. Arithmetic and Branches 40  MU 40 points
4. Stack and Interrupts 40  AL 40 points
5. Coding Problem 40  DU 40 points
6. Fun Question 10  RC 10 points
Total 200 200 points

Section 1. Fundamentals

1. Write out the contents of memory that result from the following variable declarations, beginning from offset 0 in the code segment.  You may use 'A' to represent the ASCII byte for the character A.  If the contents cannot be determined put an X in the box (15 points)

Var1	dd 'kalimera'
Var2    resw 5
Var3    db 'Cool','$'
Var4    resd 4
Var5    dw 1Ah, 0Ah
CS:00 'k' 'a' 'l' 'i' 'm' 'e' 'r' 'a'
CS:08 X X X X X X X X
CS:10 X X 'C' 'o' 'o' 'l' '$' X
CS:18 X X X X X X X X
CS:20 X X X X X X X 1Ah
CS:28 00h 0Ah 00h X X X X X
CS:30 X X X X X X X X
CS:38 X X X X X X X X

2. How many memory operations will it take to read a word from address 291 and a word from address 292 in the 80286? (6 points)

Three operations

3. Fill the missing entries in the following table, which contains mappings between logical addresses and linear addresses in real mode.  All numbers given in hex (9 points)

1111:FFFF 2110F
1100:8000 19000
F000:FFFE (1 of many) FFFFE

Section 2. Memory Addressing

1. Using the register values given below (in hex), determine the memory address (or addresses) of the byte (or bytes) read or written in each of the following instructions.  Note that the problems can be solved independently (20 points)

BX=1234 DI=2000 CS=2556
BP=11CA SI=0500 DS=2000
SS=5600 SP=1030  
Instruction Linear Address(es) Read or Written
MOV AX, [DI] 22000, 22001
SUB DX, [BX+SI] 21734, 21735
PUSH SI 5702E, 5702F
ADD CX, [BP+0A00h] 57BCA, 57BCB
POP [DI+1234h] 23234, 23235, 57030, 57031

2. Write legal or illegal next to each of the following instructions and give justification for the illegal ones (20 points)

  1. MOV WORD[BX+SI], [DI]

Illegal: Cannot move from memory to memory

  1. MOV BYTE[BL], 5

Illegal: Cannot index memory with BL (a byte)

  1. MOV WORD[CS:SI], 42

Legal

  1. ADD AX, BYTE[SI]

Illegal: Mismatch of operand sizes

  1. ADD DL, [AX+BX]

Illegal: Cannot address memory with AX

Section 3. Arithmetic and Branches

1. The ECE291 TA's have a worldwide reputation for their ability to analyze and debug weird codes.  To appreciate their efforts, analyze the following code and try to determine the value of AX at the five question points in the code. (20 points)

LATER	EQU	8
NOW	EQU	4
ORANGES	EQU	9
THREE	EQU	3
...
; macro formerly known as BURGERS_AND_FRIES
%macro	EAT	2
%%SHIP
	MOV	AX, %{1}
	MOV	CX, %{2}
	ROR	AX, CL
	JC	%%SHIP
	INC	CX
%%SHIP
%endmacro
...
; some groceries
APPLES	DW	5
MANGOS	DB	7, 4, 5, 6, 3
PEACHES	DW	9, 3, 2, 1, 8
FRUITCAKE	DW 0BADh
...
; a procedure
HOME
	SUB	WORD[FRUITCAKE], THREE
	CMP	WORD[APPLES], ORANGES
	JAE	.PEAR
	ADD	WORD[APPLES], 2
	CALL	HOME
.PEAR
	MOV	AX, [FRUITCAKE]
	RET
...
EAT_HEALTHY
	EAT	0F00Dh, NOW
	; QUESTION 1: AX = 0DF00h
	EAT	CX, AX
	; QUESTION 2: AX = 4000h
	MOV	AX, [PEACHES+3]
	MOV AL, [MANGOS+3]
	; QUESTION 3: AX = 0206h
	CALL	HOME
	; QUESTION 4: AX = 0BA4h
	MOV	AX, [APPLES]
	MOV	CX, 8
.FOR_A_WHILE
	ADD	AX, CX
	LOOP	.FOR_A_WHILE
	; QUESTION 5: AX = 002Dh
	RET

2. Write code to multiply a 8-bit unsigned integer stored in AL by 11.  Using the MUL instruction will cost you 50% of the points (10 points)

	xor	ah, ah
	mov	bx, ax
	shl	bx, 1
	add	ax, bx
	shl	bx, 2
	add	ax, bx

Note: many other versions were accepted. The high halves did not need to be used for full credit.

3. Suppose the register AX contains a pair of packed binary values.  The low order 4 bits contain a value in the range 0..15 and the high order 12 bits contain a value in the range 0..4095.  Write the code to set the high order 12 bits of AX to the decimal value 291 and the low order 4 bits of AX to the decimal value 10. (10 points)

	mov	ax, 291
	shl	ax, 4
	add	ax, 10
	; or
	mov	ax, 123Ah

Section 4. Interrupts and the Stack

1. We asked a lousy programmer to write a procedure that computes the area of a trapezoid.  We would like to call the procedre with a far call from our assembly program.  We pass three parameters to the procedure: b1, b2, and h.  These are unsigned 16-bit numbers and they are passed to the procedure using the convention of C compilers for parameter passing.  To make his life easier, we told him that the area of a trapezoid is computed as (b1+b2)h / 2 and that we don't mind so much about accuracy.  Here's what our programmer came up with.  The next day he was fired.

Find all errors in the following code. (15 points)

Trapezoid:
	PUSH	BP
	PUSH	CX
	MOV	SP, BP	Should be MOV BP, SP
	MOV	AX, [BP+4]	Offsets are incorrect.  Should be 8, 10, 12 (unless bp set before push cx)
	ADD	AX, [BP+6]
	MUL	WORD[BP+8]
	MOV	CX, 2
	DIV	CX	(easier done with a shift...)
	POP	BP	Pops are reversed
	POP	CX
	RET	Should be RETF, the far return

2. What is the interrupt vector table and where is it stored in memory? (5 points)

It is the jump table of far ISR addresses. It's stored in the first 1024 bytes of memory

3. Select True or False for each of the following statements. (10 points)

4. Explain in a few words the steps taken to install and uninstall a new ISR for a hardware interrupt. (10 points)

To install: Save the old address and write the new address into the vector table.

To remove: Write the saved old address back into the vector table.

Section 5. Coding Problem

Write a procedure which takes in seeral single-digit numbers via kbdin and returns their average.  For full points, make sure to ignore any keypresses that are not digits.  Write this procedure as you would in a machine problem. (40 points)

extern kbdin ; returns ASCII value in al
...

AvgNDigits:
; Reads in CX single-digit numbers and averages them into AX
;
; Inputs: CX = number of digits to average
; Outputs: AX = average of the CX digits
; Calls: kbdin to read each digit

  push  dx
  push  cx
  xor   dx, dx       ; start sum from 0
.next
  xor   ah, ah
  call  kbdin        ; get a digit
  cmp   al, '0'
  jl    .next        ; ignore non-digits
  cmp   al, '9'
  jg    .next
  sub   al, 30h
  add   dx, ax       ; sum digits
  loop  .next
  pop   cx           ; retrieve count
  mov   ax, dx
  xor   dx, dx
  div   cx           ; average
  pop   dx
  ret

Section 6. Fun Question

Write what the following two sets of instructions do using no more than 10 words (5 in each question).  Each word in excess of 10 will cost you a point. (10 points)

XOR AX, BX
XOR BX, AX
XOR AX, BX
Answer: Swaps AX and BX
CBW
XOR AL, AH
SUB AL, AH
Answer: Takes absolute value of AL