My Compiler is 1.75x smaller than C compiler

Sabuhi
1 min read English

First, I'm a begginer. QBE compiler uses libc libraries for C functions. But C libraries are heavier than my custom functions.

It doesn't mean my assembly code is perfect. Howover it's simple. Just look at this code. It's doesn't use libc.

export function w $main() {               
    @start 
        call $print(l $str)
        call $exit(w 0)
        ret
}

data $str = { b "hello world\n", b 0 }

And QBE generates this assembly code.

.text
.globl main
main:
	pushq %rbp
	movq %rsp, %rbp
	leaq str(%rip), %rdi
	callq print
	movl $0, %edi
	callq exit
	leave
	ret
.type main, @function
.size main, .-main
/* end function main */

.data
.balign 8
str:
	.ascii "hello world\n"
	.byte 0
/* end data */

.section .note.GNU-stack,"",@progbits

As we can see, leaq means loads the address of str into %rdi register. From this I understood my print function must use %rdi register for the first parameter.

I've written print function and linked them together. We need to understand syscalls.

.intel_syntax noprefix
.global print
.text

print:
    mov rsi, rdi   
    mov rax, 1      
    mov rdi, 1      
    mov rdx, 12    
    syscall
    ret

If you don't understand, You can check out this website https://www.chromium.org/chromium-os/developer-library/reference/linux-constants/syscalls/

Now let's compare the C code with the custom assembly code. app--> Custom Assembly result,

main --> Compiled C Executable file.

the same result, but the sizes are different. Our Compiler 1.75x smaller than C compiler

But how? Because we don't use llvm. We are using a simple compiler called qbe. And We are writing custom assembly sometimes.

0

Responses (0)

Sign in to leave a response.

No responses yet. Be the first.