about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--subx/012elf.cc19
-rwxr-xr-xsubx/ex9bin147 -> 144 bytes
-rw-r--r--subx/ex9.subx12
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