Introduction to Computer Science:
Solutions to Quiz 3
    Sewickley Academy, 2000-2001

See Course Home Page.

1.  Write an assembly language program which swaps the values in registers R8 and R9.

move RA,R9  ; store R9 in temporary RA
move R9,R8  ; store R8 in R9
move R8,RA  ; store *original* R9 value in R8
2.  Write an assembly language program which loads register RD with the smallest of the values stored in registers RA, RB, and RC.
  ;; Test whether RA is smallest by verifying it is smaller
  ;; than RB and RC, and, if so, load RD with RA and halt

  move  R0,RA
  jmpLE RB<=R0,RANotSmallest
  jmpLE RC<=R0,RANotSmallest
  move  RD,RA
  jmp   Done
RANotSmallest:

  ;; Here, we know RA is not smallest, so we know the smaller
  ;; of RB and RC is the smallest, so we compare them and load
  ;; the smaller of the two into RD and halt.

  move  R0,RB
  jmpLE RC<=R0,RCSmallest
  move  RD,RB
  jmp   Done
RCSmallest:
  move  RD,RC
Done:
  halt

3.  Write an assembly language program which loads register R3 with 0x01 if the value in register R4 is odd, and 0x00 otherwise. (Hint:  in all odd numbers the rightmost bit is set to 1 -- you may want to use an and operator to select that bit)
load R5,0x01  ; R5 = constant 0x01
and  R3,R4,R5 ; R3 = 1 if R4 is odd, 0 otherwise
halt
4.  Consider the following assembly language program:
  load R1,0x01      ;; 0x01 = 1
  load R2,0x19      ;; 0x19 = 25
  load R5,0x41      ;; 0x41 = 65 = ASCII('A')
  move R0,R5
  addi R0,R0,R2
DoMore:
  move RF,R5
  addi R5,R5,R1
  jmpLE R5<=R0,DoMore
  halt

State very clearly, in as few words as possible, precisely what this program does.

Prints the uppercase letters:  ABCD...WXYZ

5.  When the program in problem 4 is assembled (loaded into SimpSim), how many bytes will the resulting machine language program take up?  Very briefly explain.
18 bytes -- 2 per line (and labels do not count, as they disappear as part of assembling into machine language).
6.  True or False:
a)  The Arithmetic Logic Unit, or ALU, includes a Program Counter and an Instruction Register, and communicates with the CPU across the bus.

FALSE.  This statement has multiple problems:  First, the ALU does not include the PC or IR (those are in the control unit); Second, the ALU is on the CPU, so it does not communicate with the CPU across the bus (main memory does so).

b) It is possible to write the game of Nim in assembly language.

TRUE.  In fact, we did so.

c) It is possible to write any computer game (even chess!), and any computer program (even a word processor or a web browser!) in assembly language.

TRUE.  In fact, as we discussed, when we use "higher-level" programming languages such as C++, these are just translated (compiled) into assembly language, which itself is assembled into machine language, which in the end is the only language machines understand.

d)  Assembly language depends on the kind of chip in a computer, so PC's using Intel chips (such as the Pentium) do not use the same assembly language as Macs using Motorola chips.

TRUE.  This is another compelling reason to user "higher-level" languages such as C++ or Java rather than a very low-level language like assembly language:  for the higher-level languages, there exist translators (compilers) for each major chip type, so you can write your C++ program once, and then compile it for Intel PC's using one compiler and then compile the same code again for Mac's using another compiler.

See Course Home Page.