From f7d14ea364fc7d592cfe6e5664b3bc7ad72b7804 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 18 Jul 2020 21:49:29 +0200 Subject: playing with small elf binaries in assembly --- ecomp-c/README | 5 +++++ ecomp-c/asm-i386.c | 14 ++++++++++++-- ecomp-c/tests/asm-i386/elf.asm | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 ecomp-c/tests/asm-i386/elf.asm diff --git a/ecomp-c/README b/ecomp-c/README index 4e1e88c..0f8e907 100644 --- a/ecomp-c/README +++ b/ecomp-c/README @@ -122,6 +122,11 @@ C compiler). We prefer the hierarchical approach one file per assembly file, load at fixed ORG (sort of in a.out style). +another assembler, 0:15:00 + +neested functions allow more efficient compiling as local scopes can +be dropped and their local symbols. + syscalls -------- diff --git a/ecomp-c/asm-i386.c b/ecomp-c/asm-i386.c index 431ed52..fc91130 100644 --- a/ecomp-c/asm-i386.c +++ b/ecomp-c/asm-i386.c @@ -70,8 +70,8 @@ enum { }; static int DEBUG_GETCHAR = 0; -static int DEBUG_SCANNER = 0; -static int DEBUG_PARSER = 0; +static int DEBUG_SCANNER = 1; +static int DEBUG_PARSER = 1; /* scanner */ @@ -88,6 +88,8 @@ typedef enum { S_comma, S_lbrak, S_rbrak, + S_plus, + S_minus, S_eof } S_Symbol; @@ -104,6 +106,8 @@ static char *symname[S_eof+1] = { ",", "[", "]", + "+", + "-", "eof" }; @@ -385,6 +389,12 @@ static S_Symbol getSym( void ) number( ); s = S_number; break; + case '-': + s = S_minus; + break; + case '+': + s = S_plus; + break; case 'f': identifier( ); if( strcmp( ident, "format" ) == 0 ) { diff --git a/ecomp-c/tests/asm-i386/elf.asm b/ecomp-c/tests/asm-i386/elf.asm new file mode 100644 index 0000000..16f40e1 --- /dev/null +++ b/ecomp-c/tests/asm-i386/elf.asm @@ -0,0 +1,39 @@ +format binary +use32 +org $08048000 +ehdr: +db $7F, "ELF" ; e_ident: magic +db 1 ; EI_CLASS: ELFCLASS32 +db 1 ; EI_BYTE: ELFDATA2LSB (little endian, 2's complement) +db 1 ; version of the object file format +db 0 ; EI_VERSION: EV_CURRENT, ABI version +dd 0, 0 ; EI_PAD: padding +dw 2 ; e_type: executable +dw 3 ; e_machine: Intel 80386 +dd 1 ; e_version: current version +dd _start ; e_entry: entry address to _start +dd phdr - $$ ; e_phoff: program header offset at phdr - current position +dd 0 ; e_shoff, no section header table +dd 0 ; e_flags +dw ehdrsize ; e_hsize: header size +dw phdrsize ; e_phentsize: size of a program header entry +dw 1 ; e_phnum: 1 entry in the program header table +dw 0 ; e_shentsize +dw 0 ; e_shnum +dw 0 ; e_shstrndx +ehdrsize = $ - ehdr +phdr: +dd 1 ; e_type: PT_LOAD +dd 0 ; e_offset +dd $$ ; p_vaddr +dd 0 ; p_paddr +dd filesize ; p_filesz +dd filesize ; p_memsz +dd 5 ; p_flags: Read & Execute +dd 0x1000 ; p_align +phdrsize = $ - phdr +_start: +mov eax, 1 +mov ebx, 42 +int $80 +filesize = $ - $$ -- cgit v1.2.3-54-g00ecf