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.

No comments: