CS 330 Lecture 15 – Assembly Cont’d

Agenda

  • what ?s
  • line at a time
  • dealing with memory
  • conditional statements
  • calling functions

TODO

  • 1/4 sheet. Pick one of:
    • Write an assembly program that gets a number from the user and prints out just its least significant byte.
    • Read Stop Misquoting Donald Knuth!

Line at a Time

  1: .section .data
  2: out_message:
  3:   .asciz "What's the difference between %d and %d? %d.\n"
  4: prompt_message:
  5:   .asciz "Enter two numbers: "
  6: in_format:
  7:   .asciz "%d %d"
  8: a:
  9:   .long 20
 10: b:
 11:   .long 4
 12: 
 13: .section .text
 14: .globl main
 15: 
 16: main:
 17:   pushl $prompt_message
 18:   call printf
 19:   addl $4, %esp
 20: 
 21:   pushl $b
 22:   pushl $a
 23:   pushl $in_format
 24:   call scanf
 25:   addl $12, %esp
 26: 
 27:   movl a, %eax
 28:   subl b, %eax
 29: 
 30:   pushl %eax
 31:   pushl b
 32:   pushl a
 33:   pushl $out_message
 34:   call printf
 35:   addl $16, %esp
 36: 
 37:   pushl $0
 38:   call exit
  1: .section .data
  2: message:
  3:   .ascii "One past %d is %d.\0"
  4: 
  5: .section .bss
  6:   .lcomm a, 4
  7:   .lcomm b, 4
  8: 
  9: .section .text
 10: .globl main
 11: 
 12: main:
 13:   movl $12, %eax
 14:   movl %eax, a
 15:   movl %eax, b
 16:   incl b
 17: 
 18:   pushl b
 19:   pushl a
 20:   pushl $message
 21:   call printf
 22:   addl $12, %esp
 23: 
 24:   pushl $0
 25:   call exit

Code

Haiku

on the origin of magic
Inside magic things
You’ll find ordinary parts
Working together

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *