% 1997 Fall, Midterm WITH SOLUTIONS, for V22.0201.002

\documentclass{article}
\setlength{\oddsidemargin}{0pt}
\setlength{\evensidemargin}{0pt}
\setlength{\topmargin}{-.5in}
\setlength{\textwidth}{6.1in}
\setlength{\textheight}{8.5in}
\def\baselinestretch{1.6}
\newcommand{\pts}[1]{{\bf\ (#1 POINTS)\\}}
\newcommand{\ans}{{\bf\ ANS:\\}}

\newenvironment{tightlist}[1]{
\begin{list}{#1}{
\setlength{\topsep}{0in}
\setlength{\partopsep}{0in}
\setlength{\parsep}{0in}
\setlength{\parskip}{0in}
\setlength{\itemsep}{0in}
}
}{\end{list}}


\begin{document}
\begin{center}
\large
Yap \hfill    V22.0201.002, Computer Systems Organization I  \hfill        Fall 1997\\
\end{center}
\begin{center}
\Large
MIDTERM EXAM + SOLUTIONS\\
\large
For full credit show all rough work and justify all answers.
\\
% Write directly on the exam.
You are allowed 2 pages (=1 sheets) of 8.5''x11'' set of notes.
\end{center}

\normalsize

% \def\baselinestretch{1.8}
OVERALL COMMENTS:  Decided to give students a choice
of doing question 8 or 9, and go give 30 points to either
question.  Question 8 seems a lot shorter to do (this hint
was given).   5 students did this test within 35 minutes,
another 9 did it within 45 minutes.   So I guage it as
an easy exam.

\begin{enumerate}

\item
\pts{10}
Name the three kinds of buses.

\ans Control, Data and Address Buses

% \vskip 1.in

\item
\pts{5}
The ALU is found in:
\begin{enumerate}
\item The bus interface unit
\item The memory unit
\item The CPU
\item None of the above
\end{enumerate}

\ans (c) The CPU.

\item
\pts{5+5}
(a) How many kilobytes are there in a segment? 
\\
(b) What is the offset of the physical address
0ABCDEh in the segment whose number is 9FF9h?

\ans  (a) $64$K (b) 0BD4Eh

\item
\pts{5+5+5}
\\ (a)
Convert 0BADh to binary.
\\ (b)
Convert 101010b to decimal.
\\ (c)
Convert 698d to hexadecimal.

\ans (a) $1011,1010,1101$b (b) $42$d (c) 2BAh.

\item
\pts{5+5}
Suppose we use 6 bits to represent signed numbers.
Answer in decimal.
\begin{enumerate}
\item What is the largest positive number that can be represented?
\item What number does the bit pattern 110100 represent? 
\end{enumerate}

\ans  (a) $2^5 -1 = 31$, (b) $-12$.

% \pagebreak

\item
\pts{10}
Which of the following 7 names are valid in our Assembler?
\begin{verbatim}
    _here_, 1and2, my.val, isn't_it?, yap@nyu, %$?, Johnson&Johnson
\end{verbatim}

\ans  \verb+ _here_, yap@nyu, %$?+
(Remark: TASM did not complain about \verb+Johnson&Johnson+!)

\item
\pts{5+5}
Assume a program has the following declarations at the beginning of the
data segment, i.e. at offset 0000h.
\begin{tightlist}{}
\item \verb+    var1  DB  20d+
\item \verb+    var2  DW 0ABCDh, 3 dup (?)+
\item \verb+    var3  DW 'ABCD'+
\end{tightlist}
(a) What is the offset of \verb+var3+?
\\
(b) What is in register AX after the execution of the following:
\\
	\verb+     mov AX, word prt var1+

\ans (a) offset=9, (b) AX=0CD14h.  (Note: 20d=14h)

\item
\pts{25}
Write a code fragment to compute the sum $1+2+3+\cdots+n$
where $n$ is an input number in register DL.  The output
should be register DH.  Note that
$n$ is non-negative and if $n=0$, the output should be $0$.
You must use the LOOP instruction to solve this.

\ans
\begin{verbatim}
	mov cl,dl		; cx is loop counter
	xor ch,ch		; cx is initialized to dl
	xor dh,dh		; dh = 0 accumulates the answer
	jcxz DONE		; if dl=0, done
	AGAIN:
	add dh,cl
	loop AGAIN
	DONE:		
\end{verbatim}

COMMENT: some of you tried to write a complete program, which
is NOT asked for.  This gets you into trouble because it takes
some effort to get a number $n$ into DL.  There is a good
reason why we only ask for a ``code fragment''!  

Many students make the mistake of not initializing
the loop counter (register CX).  When you do, you sometimes
do ``mov CX,DL'' which is wrong.  Some of you tried to
fix this using ``mov CX, WORD PTR DL'', but this is
also not the proper use of WORD PTR!  The correct way to initialize CX
is as shown above, i.e.,
	\begin{verbatim}
	mov cl,dl
	xor ch,ch
	\end{verbatim}
Other problems: using illegal labels like (LOOP, END).
Some of you never bothered to put ":" after labels (-2 points)
or even write the ":" before the label name!!
Others simply do not understand the LOOP instruction.

Actually, there is a shorter solution without using LOOP
(but that is not what we wanted to test in this question): 
\begin{verbatim}
	xor dh,dh		; dh = 0 accumulates the answer
	AGAIN:
	add dh,dl
	dec dl
	jnz AGAIN
\end{verbatim}


\item
\pts{30}
Write a complete assembly language program to print
the contents of register BH in hexadecimal.
Here is an outline of the program to be implemented:
\begin{verbatim}
        0. Move any value (say, 97d) into BH
                (you can change this value each time you assemble the program).
        1. Move BH to DL.
        2. Shift DL 4 times to the right.
        3. If DL<10, then convert it to characters in '0',...,'9' (by adding 30h).
        4. If DL>=10, then convert it to characters in 'A',...,'F' (by adding 41h).
        5. Output the character in DL (using function 2, INT 21h).
        6. Move BH to DL.
        7. Clear the high order 4 bits of DL.
        8. Do one more time: steps 3 to 5.
\end{verbatim}

\ans
\begin{verbatim}
	title hex output
	; input in BH
	.model small
	.stack 100h
	.data
	var db 97		; change 97 to anything you want
	.code
	main proc
;
	mov ax,@data
	mov ds,ax
; OUTPUT FIRST HEX DIGIT
	mov bh, var
	mov dl, bh
	mov cl, 4
	shl dl, cl
	cmp dl, 10
	ja  BIG1
	add dl, 30h
	jmp DONE1
BIG1:	add dl, 41h
DONE1:	mov ah,2
	int 21h
; OUTPUT NEXT HEX DIGIT
	mov dl, bh
	and dl, 0Fh		; clear the high order 4 bits
	cmp dl, 10
	ja  BIG2
	add dl, 30h
	jmp DONE2
BIG2:	add dl, 41h
DONE2:	mov ah, 2
	int 21h
; DOS EXIT
	mov ax, 4c00h
	int 21h
	main endp
	end main
\end{verbatim}

\end{enumerate}
\end{document}

