Hello World! An Invitation to Computer Science

Lecture Slides

Translation from Assembly Language to Machine Code
Thursday March 13, 2008


Key ideas from last time


Another conditional example

in pseudocode:
  input x
  if x < 5
      yx + 5
  else
      yx - 5
  output y
an now in assembly language:
         .begin
         IN x
         -- test if x < 5
         LOAD five
         COMPARE x
         JUMPLT xlt 
         -- if not, set y ← x - 5
         LOAD x
         SUBTRACT five
         STORE y
         JUMP done
         -- if so, set y ← x + 5
   xlt:  LOAD x
         ADD five
         STORE y
         -- either way, output y
   done: OUT y
         HALT
   x:    .data 0
   y:    .data 0
   five: .data 5
         .end

Another loop example

Repeatedly have user enter numbers until they enter zero and then output sum of those numbers.

         .begin
         CLEAR sum
   loop: IN x
         LOAD x
         COMPARE zero
         JUMPEQ done
         ADD sum
         STORE sum
         JUMP loop
   done: OUT sum
         HALT
   sum:  .data 0
   x:    .data 0
   zero: .data 0
         .end

How does computer execute assembly program?


What does this do?

  .begin
a: JUMP a
  HALT
  .end

What we cannot do in this architecture


What does this do?

        .begin
  loop:  LOAD three
         COMPARE i
         JUMPGT done
         LOAD sum
  inc:   ADD x
         STORE sum
         INCREMENT i
         INCREMENT inc
         JUMP loop
  done:  OUT sum
         HALT
  sum:   .DATA 0
  i:     .DATA 1
  three: .DATA 3
  x:     .DATA 7
         .DATA 2
         .DATA 4
         .END