teaching machines

CS 330 Lecture 16 – Functions in Assembly

March 2, 2015 by . Filed under cs330, lectures, spring 2015.

Agenda

TODO

Code

boolean.s

.section .data
heads_message:
  .asciz "%d -> heads\n"
tails_message:
  .asciz "%d -> tails\n"

.section .text
.globl main

main:
  # Get number of seconds since 1/1/1970.
  pushl $0
  call time
  addl $4, %esp

  # Seed that PRNG.
  pushl %eax
  call srand
  addl $4, %esp

  call rand

  subl $4, %esp
  movl %eax, (%esp)

  andl $1, %eax
  cmp $0, %eax

  je heads

tails:
  pushl (%esp)
  pushl $tails_message
  call printf
  addl $8, %esp
  
  jmp quit 

heads:
  pushl (%esp)
  pushl $heads_message
  call printf
  addl $8, %esp

quit:
  pushl $0
  call exit

repeated_addition.s

.section .data
out_format:
  .asciz "The product of %d and %d is %d.\n"
a:
  .long 13
b:
  .long 12

.section .text
.globl main

main:
  pushl b
  pushl a
  call mult
  addl $8, %esp

  # %eax holds the product
  pushl %eax
  pushl b
  pushl a
  pushl $out_format
  call printf
  addl $16, %esp

  pushl $0
  call exit

mult:
  pushl %ebp
  movl %esp, %ebp

  movl $0, %ecx # Our counter
  movl $0, %eax # Our accumulating total

loop:
  cmpl %ecx, 12(%ebp)
  je done

  addl 8(%ebp), %eax
  incl %ecx
  jmp loop

done:
  popl %ebp
  ret

Haiku

on the halting problem
My function just broke
Told the store, “It won’t return”
“Exactly,” they said