diff options
author | Kartik Agaram <vc@akkartik.com> | 2018-08-31 23:46:58 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2018-08-31 23:46:58 -0700 |
commit | a413105a5dc4741ccd1c364cc82609d2aa25f6c5 (patch) | |
tree | d8b0015d9a61e52aaa10f9a32717a2844eb57fcb /subx | |
parent | f1b3d7b96749165d771b279b56cc05447b7db3e0 (diff) | |
download | mu-a413105a5dc4741ccd1c364cc82609d2aa25f6c5.tar.gz |
4528 - commandline arguments working natively
Turns out I had totally the wrong idea. The stack at the start of the program doesn't contain 2 words, one for argc and a second for argv that must then be dereferenced to get to its contents (each a pointer to a string). It contains a word for argc, one for argv[0], another for argv[1], and so on. Many thanks to Jeremiah Orians and the #bootstrappable channel on freenode for showing me https://github.com/oriansj/mescc-tools/blob/master/test/test5/exec_enable_amd64.M1 which set me straight. I could just pop the args like that example does, but it seems slightly more elegant, given the current calling convention, to assume the imaginary caller handles the popping.
Diffstat (limited to 'subx')
-rw-r--r-- | subx/012elf.cc | 19 | ||||
-rwxr-xr-x | subx/ex9 | bin | 147 -> 144 bytes | |||
-rw-r--r-- | subx/ex9.subx | 12 |
3 files changed, 9 insertions, 22 deletions
diff --git a/subx/012elf.cc b/subx/012elf.cc index 78131090..f82b48aa 100644 --- a/subx/012elf.cc +++ b/subx/012elf.cc @@ -68,26 +68,18 @@ void load_elf_contents(uint8_t* elf_contents, size_t size, int argc, char* argv[ // initialize args on stack // no envp for now -//? cerr << ARGV_POINTER_SEGMENT << " at " << Reg[ESP].u-4 << '\n'; - push(ARGV_POINTER_SEGMENT); -//? cerr << argc-2 << " at " << Reg[ESP].u-4 << '\n'; - push(argc-/*skip 'subx_bin' and 'run'*/2); - // initialize arg data - // we wastefully use 2 whole pages of memory for this + // we wastefully use a separate page of memory for argv uint32_t argv_data = ARGV_DATA_SEGMENT; - uint32_t argv_pointers = ARGV_POINTER_SEGMENT; - for (int i = /*skip 'subx_bin' and 'run'*/2; i < argc; ++i) { -//? cerr << "pointer: " << argv_pointers << " => " << argv_data << '\n'; - write_mem_u32(argv_pointers, argv_data); - argv_pointers += sizeof(uint32_t); - assert(argv_pointers < ARGV_POINTER_SEGMENT + SEGMENT_SIZE); + for (int i = argc-1; i >= /*skip 'subx_bin' and 'run'*/2; --i) { + dbg << "push " << argv_data << end(); + push(argv_data); for (size_t j = 0; j <= strlen(argv[i]); ++j) { -//? cerr << " data: " << argv[i][j] << " (" << NUM(argv[i][j]) << ")\n"; write_mem_u8(argv_data, argv[i][j]); argv_data += sizeof(char); assert(argv_data < ARGV_DATA_SEGMENT + SEGMENT_SIZE); } } + push(argc-/*skip 'subx_bin' and 'run'*/2); } void push(uint32_t val) { @@ -134,7 +126,6 @@ void load_segment_from_program_header(uint8_t* elf_contents, size_t size, uint32 const int CODE_START = 0x08048000; const int SEGMENT_SIZE = 0x1000; const int AFTER_STACK = 0x0804c000; -const int ARGV_POINTER_SEGMENT = 0x0804d000; const int ARGV_DATA_SEGMENT = 0x0804e000; :(code) void initialize_mem() { diff --git a/subx/ex9 b/subx/ex9 index c7f1e9e6..47c79e3c 100755 --- a/subx/ex9 +++ b/subx/ex9 Binary files differdiff --git a/subx/ex9.subx b/subx/ex9.subx index b8482c43..9ea547f1 100644 --- a/subx/ex9.subx +++ b/subx/ex9.subx @@ -2,16 +2,15 @@ # # To run: # $ subx translate ex9.subx ex9 -# $ subx run ex9 abc d e f g h +# $ subx run ex9 abc de fghi # Expected result: # $ echo $? # 3 # length of 'abc' # # At the start of a SubX program: # argc: *ESP -# argv: *(ESP+4) -# argv[0]: *argv ("ex9" in this case) -# argv[1]: *(argv+4) +# argv[0]: *(ESP+4) +# argv[1]: *(ESP+8) # ... # Locals start from ESP-4 downwards. @@ -20,10 +19,7 @@ # op subop mod rm32 base index scale r32 # 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes # var s = argv[1] (EBX) - # var s = argv - 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 3/r32/EBX 4/disp8 . # copy *(ESP+4) to EBX - # s = *(s+4) - 8b/copy 1/mod/*+disp8 3/rm32/EBX . . . 3/r32/EBX 4/disp8 . # copy *(EBX+4) to EBX + 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 3/r32/EBX 8/disp8 . # copy *(ESP+8) to EBX # call ascii_length(EBX) # prepare call 55/push . . . . . . . . # push EBP |