Hello World! An Invitation to Computer Science

Lecture Slides

Assembly Language
Monday March 10, 2008


Key ideas from last time


Moore's Law


Non-von Neumann architectures


Virtual machines


What assembly language offers


Example: 2 + 4 = 6 (revisited)

        .BEGIN
        LOAD X    -- R <-- contents of address X
        ADD Y     -- R <-- R + contents of Y
        STORE Z   -- contents of Z <-- R
        OUT Z     -- output contents of Z
        HALT      
   X:   .DATA 2 
   Y:   .DATA 4
   Z:   .DATA 0
        .END
A possible translation:
 addr       data          hex       
 ---------------------------------------------
 000   0000000000000101   0005  LOAD X
 001   0011000000000110   3006  ADD Y
 010   0001000000000111   1007  STORE Z
 011   1110000000000111   E007  OUT Z
 100   1111000000000000   F000  HALT   
 101   0000000000000010   0002    
 110   0000000000000100   0004
 111   0000000000000000   0000

An algorithm we have seen

  input n
  sum ← 0
  i ← 1
  while in
      sumsum + i
      ii + 1
  output sum
but now in assembly:
         IN n
   loop: LOAD n
         COMPARE i
         JUMPGT done
         LOAD sum
         ADD i
         STORE sum
         INCREMENT i
         JUMP loop
   done: OUT sum
         HALT
   n:    .data 0
   sum:  .data 0
   i:    .data 1

From machine to assembly

Consider this machine code:
  1101000000001001
  1101000000001010
  0000000000001010
  0111000000001001
  1011000000000111
  1110000000001010
  1000000000001000
  1110000000001001
  1111000000000000

We can translate it to assembly
  one insruction at a time:

  1101000000001001 

  opcode    address
  1101    000000001001 

  IN 9
and so on:
  1101000000001010    →   IN 10
  0000000000001010    →   LOAD 10
  0111000000001001    →   COMPARE 9
  1011000000000111    →   JUMPLT 7
  1110000000001010    →   OUT 10
  1000000000001000    →   JUMP 8
  1110000000001001    →   OUT 9
  1111000000000000    →   HALT

What does this program do?

        .begin
        IN x
        IN y
        LOAD y
        COMPARE x
        JUMPLT xmin
        OUT y
        JUMP done
  xmin:	OUT x
  done:	HALT
  x:	.data 0
  y:	.data 0
        .end  IN x