Date: Tue, 05 Nov 1996 20:51:37 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Fri, 13 Sep 1996 18:53:11 GMT Content-length: 5710 Q1.m.html

CS354, Fall 1996

Quiz 1, Sections 1 & 3

Name (printed): Name (signed):

 

(1) (2 points) What changes would need to be made to the SAL code shown in figure 2.7 on page 39 of the text in order to get it to run correctly?

ANSWER

(a) add "ch: .byte" to the .data section,
(b) change "loop" to "__start"

(2, ver 1) (4 points) Translate the following C statement into SAL code:

A = 1 + B * ( A / B - 5 );

ANSWER

div     temp, A, B
sub     temp, temp, 5
mul     temp, B, temp
add     A, 1, temp

(2, ver 2) (4 points) Translate the following C statement into SAL code:

A = B + 55 / ( A * B - 1 );

ANSWER

mul	temp, A, B
sub	temp, temp, 1
div	temp, 55, temp
add	A, B, temp

(3, ver 1) (2 points) Assuming "a" is of type ".word," what is printed by the following SAL code:

move    a, 468
div	a, a, 10
put	a
rem	a, a, 10
put	a

ANSWER

466

(3, ver 2) (2 points) Assuming "a" is of type ".word," what is printed by the following SAL code:

move    a, 468
rem	a, a, 10
put	a
div	a, a, 3
put	a

ANSWER

82

(4) (5 points) Explain the difference between "b x" and "b (y)"

ANSWER

"b x" causes a branch to the line of the code labeled by "x" while "b (y)" causes a branch to the line of the code whose address has been stored in variable "y"

(5, ver 1) (10 points) Write a complete SAL program that will add all of the even numbers from 8 to 72 except for multiples of 3. The program should print the result and label it using the string "sum = ." For example, if the sum were 1234, the program would print:

sum = 1234

ANSWER

          .data
sum:      .word                     # running sum
n:        .word     8               # n = 8 to 72, even
k:        .word                     # k = n mod 3
msg:      .asciiz   "sum = "
 
          .text
__start:  add     sum, sum, n
next:     add     n, n, 2           # keep n even
          rem     k, n, 3
          beqz    k, next           # skip multiples of 3
          ble     n, 72, __start
          puts    msg
          put     sum
          putc    '\n"
          done

(5, ver 2) (10 points) Write a complete SAL program that will add all of the odd numbers from 9 to 105 except for multiples of 5. The program should print the result and label it using the string "sum = ." For example, if the sum were 1234, the program would print:

sum = 1234

ANSWER

         .data
sum:     .word                      # running sum
n:       .word     9                # n = 9 to 105, odd
k:       .word                      # k = n mod 5
msg:     .asciiz   "sum = "
 
         .text
__start: add	sum, sum, n
next:    add	n, n, 2            # keep n odd
         rem	k, n, 5
         beqz    k, next            # skip multiples of 5
         ble	n, 105, __start
         puts    msg
         put     sum
         putc    '\n'
         done

(6, ver 1) (2 points) What is 3456 in Roman numeral notation?

ANSWER

3000 + 400 + 50 + 6
MMM CD L VI = MMMCDLVI

(6, ver 2) (2 points) What is 3607 in Roman numeral notation?

ANSWER

3000 + 600 + 0 + 7
MMM DC VII = MMMDCVIII

(7, ver 1) (10 points) Convert the following C code to the equivalent SAL code, assume all variables are of integer type.

if (a > b)
++a;
else if ( b == 5)
a = a * b;
else
a = 0;
printf("%d\n",a);

ANSWER

# SAL code is:
       ble     a, b, else1
       add     a, a, 1
       j       prt
else1: bne     b, 5, else2
       mul     a, a, b
       j       prt
else2: move    a, 0
prt:   put     a
       putc    '\n'

(7, ver 2) (10 points) Convert the following C code to the equivalent SAL code, assume all variables are of integer type.

if (a < b)
a = a + 2;
else if ( b != 5)
a = a / b;
else
++a;
printf("%d\n",a);

ANSWER

# SAL code is:
       bge     a, b, else1
       add     a, a, 2
       j       prt
else1: beq     b, 5, else2
       div     a, a, b
       j       prt
else2: add     a, a, 1
prt:   put     a
       putc    '\n'