code generator: react on: struct Namespace* Parser_add_new_scope(struct Parser* self); void Parser_end_scope(struct Parser* self); and on adding variables to the namespace. register order: eax ecx edx ebx esp ebp esi edi generate: STACK FRAME MOV (adr), %eax A1 adr4 MOV (adr), %ecx 8B 0D adr4 MOV (adr), %edx 8B 15 adr4 MOV (adr), %ebx 8B 1D adr4 MOV (adr), %esi 8B 35 adr4 MOV (adr), %edi 8B 3D adr4 MOV (adr), %esp 8B 25 adr4 MOV (adr), %ebp 8B 2D adr4 MOV %eax, (adr) A3 adr4 MOV %ecx, (adr) 89 0D adr4 MOV %edx, (adr) 89 15 adr4 MOV %ebx, (adr) 89 1D adr4 MOV %esi, (adr) 89 35 adr4 MOV %edi, (adr) 89 3D adr4 MOV %esp, (adr) 89 25 adr4 MOV %ebp, (adr) 89 2D adr4 MOV $0x2A, %eax B8 2A MOV $0x2A, %ecx B9 2A MOV $0x2A, %edx BA 2A MOV $0x2A, %ebx BB 2A MOV $0x2A, %esp BC 2A MOV $0x2A, %ebp BD 2A MOV $0x2A, %esi BE 2A MOV $0x2A, %edi BF 2A CMP $0x2A, %eax 83 F8 2A CMP $0x2A, %ecx 83 F9 2A ... CMP %eax, (adr) 39 05 adr4 CMP %ecx, (adr) 39 0D adr4 CMP %edx, (adr) 39 15 adr4 CMP %ebx, (adr) 39 1D adr4 CMP %esi, (adr) 39 35 adr4 CMP %edi, (adr) 39 3D adr4 CMP (adr), %eax 3B 05 adr4 CMP (adr), %ecx 3B 0D adr4 ... ADD $0x2A, %eax 83 C0 2A ADD $0x2A, %ecx 83 C1 2A ... SUB $0x2A, %eax 83 EA 2A MUL %eax F7 E0 MUL %ecx F7 E1 ... DIV %eax F7 F0 DIV %ecx F7 F1 ... CALL .. E8 EA RET C3 JE 0F 84 leE9FFFFFF JMP E9 ... INT $0x80 CD 80 PUSH %ebp 55 POP %ebp 5D MOV %esp, %ebp 89 E5 MOV (%ebp+4), %eax 8B 45 04 MOV (%ebp+4), %ecx 8B 4D 04 MOV %eax, (%ebp+4) 89 45 04 ... generator should return total amount of text bytes used so we can calculate offsets. generate same for double. generate: data segment BSS calculate total size of data. keep track of generated in-need-of-relocation points (if not using silly JMP tables). system calls: +------------------------------+------------------------------------+ | Intel Code | AT&T Code | +------------------------------+------------------------------------+ | mov eax,1 | movl $1,%eax | | mov ebx,0ffh | movl $0xff,%ebx | | int 80h | int $0x80 | | mov ebx, eax | movl %eax, %ebx | | mov eax,[ecx] | movl (%ecx),%eax | | mov eax,[ebx+3] | movl 3(%ebx),%eax | | mov eax,[ebx+20h] | movl 0x20(%ebx),%eax | | add eax,[ebx+ecx*2h] | addl (%ebx,%ecx,0x2),%eax | | lea eax,[ebx+ecx] | leal (%ebx,%ecx),%eax | | sub eax,[ebx+ecx*4h-20h] | subl -0x20(%ebx,%ecx,0x4),%eax |