Wednesday, March 18, 2009

cookie Again

After reading up on calling conventions a bit more since last time, I decided to rewrite cookie.s:

cookie.s

.testString:
.string "Would you like some cookies?"
.yesString:
.string "yes\n"
.noString:
.string "no\n"
.yesReply:
.string "Here you go!"
.noReply:
.string "Aww...why not?"
.neitherReply:
.string "What?"
.globl main
main:
pushl %ebp
movl %esp, %ebp

subl $1024, %esp

pushl $.testString
call puts
addl $4, %esp

repeat:
movl $0, -4(%ebp)

pushl stdin
pushl $1024
leal -4(%ebp), %eax
pushl %eax
call fgets
addl $12, %esp

movl -4(%ebp), %eax
shl $24, %eax
jz main_end

leal -4(%ebp), %eax
pushl %eax
pushl $.yesString
call strcasecmp
addl $4, %esp

cmpl $0, %eax
je yes_label

pushl $.noString
call strcasecmp
addl $4, %esp

cmpl $0, %eax
je no_label

neither_label:
pushl $.neitherReply
call puts
addl $8, %esp
jmp repeat

yes_label:
pushl $.yesReply
call puts
addl $8, %esp
jmp main_end
no_label:
pushl $.noReply
call puts
addl $8, %esp
main_end:
addl $1024, %esp
movl $0, %eax



movl %ebp, %esp
popl %ebp
ret

The example output is the same as last time.

No comments: