# Sample assembly language program for CSE 413 # Hal Perkins, 11/06 # Output from your compiler on similar source code will # probably be somewhat different, but the basic code # should be roughly the same and should have the same effect. .text .globl d$main # int main() { # int answer; # int m; # int n; d$main: # function prologue: pushl %ebp # save old ebp movl %esp, %ebp # set new ebp subl $24, %esp # allocate space for locals # m = get(); call get # call get (no args) movl %eax, -8(%ebp) # store m # n = get(); call get # call get movl %eax, -4(%ebp) # store n # answer = sum(m,n); movl -4(%ebp), %eax # push n pushl %eax movl -8(%ebp), %eax # push m pushl %eax call sum # call sum addl $8, %esp # pop arguments movl %eax, -12(%ebp) # store answer # answer = put(answer); movl -12(%ebp), %eax # push answer pushl %eax call put # call put addl $4, %esp # pop argument movl %eax, -12(%ebp) # store answer # return -1; movl $-1, %eax # put -1 in eax movl %ebp, %esp # restore esp popl %ebp # restore ebp ret # return # int sum(int a, int b) { # int x; # int y; # int total ; sum: # function prologue pushl %ebp # save ebp movl %esp, %ebp # set new ebp subl $16, %esp # allocate locals # x = a; movl 8(%ebp), %eax # load a movl %eax, -12(%ebp) # store x # y = b; movl 12(%ebp), %eax # load b movl %eax, -8(%ebp) # store y # total = x + y; movl -8(%ebp), %eax # load y addl -12(%ebp), %eax # add x movl %eax, -4(%ebp) # store total # return total; movl -4(%ebp), %eax # load total into eax movl %ebp, %esp # restore esp popl %ebp # restore ebp ret # return