LMU ☀️ CMSI 2210
COMPUTER SYSTEMS ORGANIZATION
HOMEWORK #3 PARTIAL ANSWERS
  1. It’s a little clearer if we use the $\uparrow$ notation, but here are both:
    • $X = A+B\,C = \overline{(\overline{A\,A})(\overline{B\,C})} = (A\uparrow A) \uparrow (B \uparrow C)$
    • $Y = \overline{A} + C = \overline{A\,(\overline{C\,C})} = A \uparrow (C \uparrow C)$
  2. $\overline{a_1}b_1 + a_0b_1\overline{b_0} + \overline{a_1}a_0\overline{b_0}$
  3. a) AND with 0xAAAAAAAA
    b) OR  with 0x00000007
    c) AND with 0x00000007
    d) OR  with 0xFFFFFFFF
    e) XOR with 0xC0000000
    f) AND with 0xFFFFFFF8
    
  4. Program that sends the values 0 through 255 out to port 0x8.
    start:  LOAD  value    ; get current value
            OUT   8        ; and write it out
            ADD   one      ; this will be the next value
            STORE value    ; store it for next time through the loop
            SUB   limit    ; will be zero only when the next value is limit
            JLZ   start    ; not yet zero means we have more to do
    end:    JUMP  end      ; only way to stop the program
    value:  0
    one:    1
    limit:  256            ; because we are always checking the next value
    
  5. Machine language for the previous problem.
    00000007
    30000008
    40000008
    10000007
    50000009
    E0000000
    C0000006
    00000000
    00000001
    00000100
    
  6. Computes a greatest common divisor. Assume the two inputs are read in from port 0x100. Write the result to port 0x200. We can use Euclid's algorithm: $$\textrm{gcd}(x,y) = x\:\textrm{if}\:y=0\:\textrm{else}\:\textrm{gcd}(y, x\:\textrm{mod}\:y)$$
            IN    0x100
            STORE x
            IN    0x100
            STORE y
    top:    LOAD  y        ; if y == 0
            JZ    done     ;     return x
            STORE old_y    ; else
            LOAD  x
            MOD   y
            STORE y        ;     next_y = x % y
            LOAD  old_y
            STORE x        ;     next_x = old y
            JUMP  top      ;     return gcd(next_x, next_y)
    done:   LOAD  x        ; This is the real "return x"
            OUT   0x200
    end:    JMP   end
    x:      0
    y:      0
    old_y:  0              ; yes we really need this
    
  7. Swaps the accumulator and memory address 0x30AA. Here’s an answer with comments explaining the approach. I'm calling the original value in the accumulator $a$ and the original value in 30AA $x$. Assume there's some other variable $t$ lying around; we’re gonna need it.
    STORE   t1         ; t1 holds a
    LOAD    0x30AA
    STORE   t2         ; t2 holds x
    LOAD    t1
    STORE   0x30AA     ; now 30AA holds original value of a
    LOAD    t2         ; now accumulator holds x
    

    There is an xor-style solution, too:

    XOR     0x30AA     ; acc holds a xor x
    STORE   t          ; t holds a xor x
    XOR     0x30AA     ; acc holds a xor x xor x, which is a
    STORE   0x30AA     ; 30AA holds a
    XOR     t          ; now accumulator holds a xor a xor x, which is x, all done!
    
  8. Jump to address 0x837BBE1 if the value in the accumulator is greater than or equal to 0.
    JGZ     0x837BBE1
    JZ      0x837BBE1
    
  9. On macOS:
    whatsup.asm
              global     start
              section    .text
    start:
              mov        rax, 0x2000004    ; system call for write
              mov        rdi, 1
              mov        rsi, phrase       ; address of string to output
              mov        rdx, 19           ; number of bytes in string (UTF-8 encoded)
              syscall                      ; invoke OS to write
    
              mov        rax, 0x2000001    ; system call for exit
              xor        rdi, rdi          ; exit code 0
              syscall                      ; invoke OS to exit
    
              section    .data
    phrase:
              db         '最近怎么样?', 10
    
  10. The values in r8 and r9 are swapped. Here’s why:
    Instructionr8r9
    xy
    xor r8, r9x xor yy
    xor r9, r8x xor yy xor (x xor y)
    x
    xor r8, r9x xor y xor x
    y
    x