Thursday, March 19, 2009

More cookie

Dissatisfied with how my most recent attempt handled strings, I decided to fix it a bit. Thank goodness for GCC's -S option:

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

movzbl -4(%ebp), %eax
testb %al, %al
je 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

Yet again, the console output is the same as before.

No comments: