Showing posts with label x86 Assembly. Show all posts
Showing posts with label x86 Assembly. Show all posts

Thursday, March 19, 2009

cookie64!

The next in the cookie legacy: cookie for x86-64 systems (cookie64). The calling conventions of the x86-64 are considerably different from those of the x86. For one, to pass up to six arguments you use the registers %rdi, %rsi, %rdx, %rcx, %r8d, and %r9d in that order. To pass any more, just push the arguments onto the stack. The end result of this choice: greater speed due to fewer memory operations.

Here's the actual code:

cookie64.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:
pushq %rbp
movq %rsp, %rbp

subq $1024, %rsp

movq $.testString, %rdi
call puts

repeat:
movl $0, -8(%rbp)

movq stdin(%rip), %rdx
movq $1024, %rsi
leaq -8(%rbp), %rdi
call fgets

movzbq -8(%rbp), %rax
testb %al, %al
je main_end

leaq -8(%rbp), %rsi
movq $.yesString, %rdi
call strcasecmp

cmpq $0, %rax
je yes_label

movq $.noString, %rdi
call strcasecmp

cmpq $0, %rax
je no_label

neither_label:
movq $.neitherReply, %rdi
call puts
jmp repeat

yes_label:
movq $.yesReply, %rdi
call puts
jmp main_end
no_label:
movq $.noReply, %rdi
call puts
main_end:
addq $1024, %rsp
movq $0, %rax



movq %rbp, %rsp
popq %rbp
ret

Still as before, the console output is the same.

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.

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.

cookie

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!