I'm running Arch Linux on an x86_64 system with a patched 2.6.33 kernel for bootsplashing, shouldn't affect what I'm doing. I'm trying to print out a number to the screen. I'm fairly new to assembly, and I don't really know my way around convention or other things to make it easier, so if there's a better way to do any of this stuff, please let me know. Anyway, my function produces a string as expected on the stack. Analyzing with gdb at the time right before the system call, %rdx is the correct length and %rcx is a null terminated string ending with \n containing the number, but the system call doesn't print it. I don't know what I'm doing wrong. If you could, please help me out. Thank you very much. Here's the code:
.section .data
.globl main
.type main,@function
.section .text
main:
pushq %rbp
movq %rsp,%rbp
subq $40,%rsp #40 bytes allocated locally
movq %rsp,%rax #set rax to the local variables
main_while: #while loop, zero that local allocation
cmpq %rax,%rbp #exit when you get back to the base pointer
jge main_while_end
andq $0,(%rax)
addq $4,%rax
jmp main_while
main_while_end:
std #set to decrement to put the number into the string backward
movq %rbp,%rdi
movq $10,%rax #put a new line
stosb
movq $13753,%rax #the number we're printing out
movq $10,%rcx #ten in %rcx for division purposes
main_do:
cdq
idivq %rcx
orb $0x30,%dl #add 0x30, 0x30 + n is the ascii code for n if n < 10
movq %rax,%rbx #save %rax / 10 in %rbx
movq %rdx,%rax #move %rax % 10 to %rax for printing
stosb
movq %rbx,%rax #put %rax / 10 back
cmpq $0,%rax
jne main_do
incq %rdi #back up one to where the string begins and save it on the stack
pushq %rdi
cld #set to increment for counting the length
#find string length by setting all %rcx bits, using scasb to scan
#scanning for '\0' then reversing %rcx after the scan and decrement
xorq %rcx,%rcx #set all rcx bits
notq %rcx
subb %al,%al #searching for NULL
repne scasb #start the scan
notq %rcx #reverse bits and subtract one
decq %rcx
movq %rcx,%rdx #length in %rdx
popq %rcx #pop saved string location to %rcx
movq $1,%rbx #file handle in %rbx, 1 == stdout
movq $4,%rax #system call in %rax, 4 == write
int $0x80 #call the kernel
movq $0,%rbx #exit status in %rbx
movq $1,%rax #system call in %rax, 1 == exit
int $0x80 #call the kernel
.end