After a little studying for my compilers course, I decided to write an assembly language program for practice. Here it is:
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:
movl %esp, %ebp
leal 4(%esp), %eax
subl $1024, %esp
pushl %eax
pushl $.testString
call puts
addl $4, %esp
repeat:
movl (%esp), %eax
movl $0, (%eax)
pushl stdin
pushl $1024
pushl %eax
call fgets
addl $12, %esp
movl (%esp), %eax
movl (%eax), %ebx
shl $24, %ebx
jz main_end
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 $1028, %esp
movl $0, %eax
ret
neil@superflex Programs % gcc -m32 -ggdb -o cookie cookie.s
% ./cookie
Would you like some cookies?
yes
Here you go!
% ./cookie
Would you like some cookies?
no
Aww...why not?
% ./cookie
Would you like some cookies?
% ./cookie
Would you like some cookies?
eoeaeoae
What?
yes
Here you go!
No comments:
Post a Comment