diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2018-08-18 19:27:10 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2018-08-18 19:27:10 +0200 |
commit | a6b830b9f542bdc65366d1d947c20aec1cbe96aa (patch) | |
tree | 0219ae27b39f4a078439e848a959e09bccd0e5f2 /crenshaw | |
parent | f6f9348fff5ca2e4ddcdfa3e6fc4da12ba213700 (diff) | |
download | compilertests-a6b830b9f542bdc65366d1d947c20aec1cbe96aa.tar.gz compilertests-a6b830b9f542bdc65366d1d947c20aec1cbe96aa.tar.bz2 |
crenshaw/emul: added printing of data section (assuming it's following the code)
Diffstat (limited to 'crenshaw')
-rw-r--r-- | crenshaw/README | 7 | ||||
-rw-r--r-- | crenshaw/emul.c | 25 | ||||
-rw-r--r-- | crenshaw/main.pas | 2 |
3 files changed, 31 insertions, 3 deletions
diff --git a/crenshaw/README b/crenshaw/README index a64a6f5..201ffcd 100644 --- a/crenshaw/README +++ b/crenshaw/README @@ -133,3 +133,10 @@ Checking for LF feels hacky. Mentions now the lexer. The mean reason for a lexer is to keep the parser simple, for instance it can work on one lookahead 'character' like GREATER_EQUALS instead of individually '>=' and '=>'. + +tutor4 + +Good point, even an expression parser can use an interpreter to +simplify constant expressions before generating code for them. +The other option is to let the programmer use the final constant +and add a comment for it. diff --git a/crenshaw/emul.c b/crenshaw/emul.c index f1dcad9..727acdb 100644 --- a/crenshaw/emul.c +++ b/crenshaw/emul.c @@ -57,14 +57,27 @@ static void dump_stack( uc_engine *uc ) { uint32_t esp; uint8_t mem[4]; + uc_reg_read( uc, UC_X86_REG_ESP, &esp ); + printf( "stack:\n" ); for( int i = esp; i < STACK_START; i += 4 ) { uc_mem_read( uc, i, &mem, 4 ); printf( "%08X: %02X%02X%02X%02X\n", i, mem[3], mem[2], mem[1], mem[0] ); } } +static void dump_memory( uc_engine *uc, uint64_t start_address, uint64_t end_address ) +{ + uint8_t mem[4]; + + printf( "data:\n" ); + for( uint64_t a = start_address; a < end_address; a += 4 ) { + uc_mem_read( uc, a, &mem, 4 ); + printf( "%08X: %02X%02X%02X%02X\n", (uint32_t)a, mem[3], mem[2], mem[1], mem[0] ); + } +} + static uint32_t mul_hash( uint64_t x, int p ) { uint32_t v = x * INT32_C( 2654435761 ); @@ -94,6 +107,7 @@ int main( int argc, char *argv[] ) cs_err cerr; size_t nof_instrs; cs_insn *instrs; + uint64_t data_start = 0; uerr = uc_open( UC_ARCH_X86, UC_MODE_32, &uc ); if( uerr != UC_ERR_OK ) { @@ -184,7 +198,14 @@ int main( int argc, char *argv[] ) for( int j = ( 16 - instrs[i].size ) * 2; j > 0; j-- ) { printf( " " ); } - printf( "%s %s\n", instrs[i].mnemonic, instrs[i].op_str ); + if( instrs[i].size == 2 && instrs[i].bytes[0] == 0 && instrs[i].bytes[1] == 0 ) { + if( data_start == 0 ) { + data_start = instrs[i].address; + } + printf( "data\n" ); + } else { + printf( "%s %s\n", instrs[i].mnemonic, instrs[i].op_str ); + } } // remember address to instrs indexes so we can get the current @@ -233,7 +254,6 @@ int main( int argc, char *argv[] ) uc_reg_write( uc, UC_X86_REG_ESP, &esp ); uint64_t address = CODE_START; - //~ cs_insn *instr = cs_malloc( cs ); bool terminate = false; int iteration = 1; @@ -278,6 +298,7 @@ int main( int argc, char *argv[] ) dump_regs( uc ); dump_stack( uc ); + dump_memory( uc, data_start, CODE_START + code_size ); if( strcmp( instrs[n].mnemonic, "hlt" ) == 0 ) { terminate = true; diff --git a/crenshaw/main.pas b/crenshaw/main.pas index 294d5a9..13499b0 100644 --- a/crenshaw/main.pas +++ b/crenshaw/main.pas @@ -283,7 +283,7 @@ begin for i := 0 to nof_symbols-1 do begin if symbols[i].sym_type = VariableType then begin EmitLabel(symbols[i].name); - EmitLn('dw 0'); + EmitLn('dd 0'); end; end; end; |