The simplest working compiler!

比较弱智,呵呵。 源程序如下: class Program { static int i; static int j; static boolean b; static void Main() { i = 3 * 5; j = i + 10; func(); print(j); } static void func() { print(i); } } 翻译成汇编码: .section .rodata format: .string "%dn" .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp subl $8, %esp movl $3, %eax movl %eax, %ebx movl $5, %eax imull %ebx, %eax movl %eax, i movl i, %eax movl %eax, %ebx movl $10, %eax addl %ebx, %eax movl %eax, j call func movl j, %eax movl %eax, 4(%esp) movl $format, (%esp) call printf leave ret .globl func .type func, @function func: pushl %ebp movl %esp, %ebp subl $8, %esp movl i, %eax movl %eax, 4(%esp) movl $format, (%esp) call printf leave ret .local i .comm i, 4, 4 .local j .comm j, 4, 4 .local b .comm b, 4, 4

March 17, 2006 · 1 min · qingbo

GAS编译的第一个汇编小程序

在Linux里写了第一个汇编小程序,呵呵,输入两个数字,计算出结果并显示。 保存为example.s, # gcc -o example example.s .section .rodata prompt: .string "Please input an integer:" format: .string "%d" oform: .string "%d + %d = %dn" .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp subl $32, %esp andl $-16, %esp movl $0, %eax subl %eax, %esp ;Instructions above have nothing meaningful. movl $prompt, (%esp) call printf ;Prompt for the first integer. movl %esp, %eax addl $20, %eax movl %eax, 4(%esp) movl $format, (%esp) call scanf ;Accept the first input. movl $prompt, (%esp) call printf ;Prompt for the second. movl %esp, %eax addl $16, %eax movl %eax, 4(%esp) movl $format, (%esp) call scanf ;Accept the second. movl 16(%esp), %eax addl 20(%esp), %eax ;Compute the sum of two integers movl %eax, 12(%esp) ;sum movl 16(%esp), %eax movl %eax, 8(%esp) ;num2 movl 20(%esp), %eax movl %eax, 4(%esp) ;num1 movl $oform, (%esp) call printf ;printf("%d + %d = %dn", num1, num2, sum); leave ret

March 16, 2006 · 1 min · qingbo

AT&T assembly syntax

记得以前是弄过Windows里的汇编的,还调用了printf。现在不知道为什么不会弄了,只好转入Linux。现转载一篇AT&T语法格式的介绍(原文 h ttp://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html ): Register naming: Register names are prefixed with “%”. To reference eax: AT&T: %eax Intel: eax Source/Destination Ordering: In AT&T syntax (which is the UNIX standard, BTW) the source is always on the left, and the destination is always on the right. So let’s load ebx with the value in eax: AT&T: movl %eax, %ebx Intel: mov ebx, eax Constant value/immediate value format: You must prefix all constant/immediate values with “$”. ...

March 11, 2006 · 3 min · qingbo

锻炼身体,好好学习

上大学后体质越来越差了,上学期竟得了气胸,唉。 最近决定每天早上打会篮球,昨天早上去了,中午没睡觉,结果今天早上没起来,中等补打了会,呵呵。坚持下去,身体会好起来的。 记得高中的时候,在家里摸那个房梁,可以超半截手指的,这次回家只摸到了一次,后来再也摸不到了。。。

March 9, 2006 · 1 min · qingbo