teaching machines

CS 330 Lecture 8 – Assembly

February 7, 2014 by . Filed under cs330, lectures, spring 2014.

Agenda

Program This

Write a little assembly program that moves a value into a register, quadruples it, and then prints out a message of the form: “4 quadrupled is 16.” Use a number of your choosing.

Code

makefile

Note to copy and pasters: makefile rules need to be indented with real tabs, not spaces.

assemblies = $(wildcard *.s)
exes = $(assemblies:.s=)

all:
  @echo $(assemblies)
  @echo $(exes)

# exe: exe.s 
# gcc -m32 -o exe exe.s 

$(exes): %: %.s
  gcc -m32 -o $@ $@.s

clean:
  rm -rf $(exes)

add.s

.section .data
a:
  .long 10
b:
  .long 20

.section .text
.globl main

main:
  movl a, %eax  # %eax = the value in the cell designated by a
  addl b, %eax  # %eax = %eax + b, %eax += b

  pushl %eax
  call exit

printf.s


.section .data
message:
  .asciz "Bow before your developer, user!\rWelcome to my program!                 \n"

.section .text
.globl main

main:
  subl $4, %esp
  movl $message, (%esp)
  call printf
  addl $4, %esp

  pushl %eax
  call exit

quadruple.s

.section .data
message:
  .asciz "%d quadrupled is %d\n"

.section .text
.globl main

main:
  movl $4, %eax   # %eax = 4
  addl %eax, %eax # %eax += %eax which doubles
  addl %eax, %eax # %eax += %eax which quadruples

  pushl %eax      # parameter 2 for printf
  pushl $4        # parameter 1 for printf
  pushl $message  # parameter 0 for printf
  call printf     # give printf control of the CPU
  addl $12, %esp  # clean up parameters

  pushl %eax
  call exit

Haiku

I liked assembly
But my computer did too
It didn’t last long