Section 9.4
Review Questions

A program with a loop

  1. How are while and if control structures implemented in assembler?
answer...
with gotos
  1. Who was the famous computer scientists who wrote the paper "Goto Considered Harmful?"
answer...
Edsgar Dijkstra
  1. What new methodology of programming appeared in the mid-1960s that replaced the older spaghetti code?
answer...
structured programming
  1. What CSC-1 instruction always appears at the "bottom" of a while loop?
answer...
JMP
  1. Which CSC-1 instruction is often used as a "do-nothing" placeholder?
answer...
NOP
  1. How many jumps must there be in every while loop?
answer...
there must be 1 at the bottom, but usually 1 at the top to break out of the while loop, otherwise it is an infinite loop
  1. Sketch out the skeleton of a while loop as implemented in CSC-1 assembler.
answer...
WHILE:    ....             ; check termination condition
          JZ    ENDWHILE   ; break out of loop if done
          ....             ; body of the loop
          JMP   WHILE      ; the instruction that makes it a loop
ENDWHILE: NOP              ; first instruction after the loop
  1. When writing CSC-1 assembler code that implements an if statement, what instruction must appear before the ELSE?
answer...
JMP
  1. How are relational comparisons such as x < y implemented in assembler?
answer...
with subtraction
  1. If two numbers are equal what value results from their subtraction?
answer...
0
  1. When you subtract A-B, what does it mean when the answer is negative?
answer...
A > B
  1. What are improvements in code called?
answer...
optimizations
  1. The gcd program in Section 9.4 does not have any I/O. Why not?
answer...
the answer is left in memory
  1. How do many computers, such as those using the Motorola 68000 chip, actually perform input and output?
answer...
with memory mapped I/O

The following questions deal with actual CSC-1 code, especially that appearing in Section 9.4. You should attempt these questions only after you've carefully studied all the code examples in the section.
  1. Write 2 lines of CSC-1 assembler which compare x != y.
answer...
LOD   X
SUB   Y
JZ    ENDWHILE
  1. Declare two integer variables called m and n. They should have the initial values 12 and 19, respectively.
answer...
M:     NUM    12
N:     NUM    19
  1. Which CSC-1 instruction should appear at the end of every program?
answer...
HLT
  1. Write a tiny chunk of CSC-1 assembler which implements an if-then statement, one that has no else clause. Here's the C statements. (Don't worry about what is in the middle where the dots are. You could use a NOP if you want to.)
               if (x != y) {
                    ....
               }
answer...
IF:     LOD    X
        SUB    Y
        JZ     ENDIF
        ...
ENDIF:  NOP
  1. Write a couple CSC-1 instructions which would implement the following C statement: (Assume that variables x and y have already been declared.)
               x = x + y;
answer...
      LOD   X
      ADD   Y
      STD   X
  1. Write a couple CSC-1 instructions which would implement the following C statement: (Assume that all variables appearing in the C have already been declared.)
               y = x - z + w;
answer...
      LOD   X
      SUB   Z
      ADD   W
      STD   Y
  1. Given the following fragment from the gcd program, why is x > y not implemented by a subtraction instruction?
        3:  IF:        JN    ELSE      ;      if (x > y)
        4:  THEN:      STD   X         ;           x = x - y;
        5:             JMP   ENDIF     ;
        6:  ELSE:      LOD   Y         ;      else
        7:             SUB   X         ;
        8:             STD   Y         ;           y = y - x;
        9:  ENDIF:     NOP             ;
answer...
because the x - y happened right before it, when we checked the termination condition of the while loop that encloses this if/then/else