Permalink
Browse files

Replaced + and ~ primitives with -.

  • Loading branch information...
1 parent 2c5e4b1 commit a4d45f4f8bbef16569e499aeb45c29d0b939f5ea @kragen committed Oct 18, 2008
Showing with 29 additions and 30 deletions.
  1. +3 −3 hello42.tbf1
  2. +1 −1 star.tbf1
  3. +4 −7 tinyboot.py
  4. +5 −0 tinyboot.s
  5. +16 −19 tinyboot1.tbf1
View
6 hello42.tbf1
@@ -16,7 +16,7 @@ Run-time primitives:
W — given an address and size on the stack, writes the specified number of
bytes to stdout
Q — exits the program with a return code of 0
-+adds two numbers
+-subtracts two numbers
@ — fetches a word from memory
! — stores to a word in memory
~ — bitwise-negates a word
@@ -58,6 +58,6 @@ v h ( ELF header, Elf32_Ehdr )
v $
-^ o @ 52 + 32 + e ! ( set entry point correctly wrt origin )
-$ h ~ 1 + + S ! ( subtract h from $ to get size of whole program )
+^ o @ 0 52 - - 0 32 - - e ! ( set entry point correctly wrt origin: o - -52 - -32 )
+$ h - S ! ( subtract h from $ to get size of whole program )
h S @ W Q
View
2 star.tbf1
@@ -5,6 +5,6 @@ v B * 8
b 33
v H # 0
v N b 10
-: g G H @ s H @ 1 + H ! ;
+: g G H @ s H @ 0 1 - - H ! ;
^ B H ! g g g g g g g g
B 8 W N 1 W Q
View
11 tinyboot.py
@@ -156,8 +156,9 @@ def write_out():
def quit():
sys.exit(0)
-def add():
- stack.append((stack.pop() + stack.pop()) & 0xFfffFfff)
+def subtract():
+ x = stack.pop()
+ stack.append((stack.pop() - x) & 0xFfffFfff)
def push_literal():
global program_counter
@@ -190,9 +191,6 @@ def store_byte():
extend_memory(addr)
memory[addr] = stack.pop() & 255
-def bitwise_not():
- stack.append(stack.pop() ^ 0xFfffFfff)
-
def less_than():
b = stack.pop()
a = stack.pop()
@@ -230,8 +228,7 @@ def loop():
'W': write_out,
'G': read_byte,
'Q': quit,
- '+': add,
- '~': bitwise_not,
+ '-': subtract,
'<': less_than,
'@': fetch,
'!': store,
View
5 tinyboot.s
@@ -19,6 +19,11 @@ syscall:
int $0x80
plus: pop %ecx
foo: add %ecx, %eax
+wrongminus:
+ pop %ecx
+ sub %ecx, %eax
+minus: sub %eax, (%esp)
+ pop %eax
lessthan:
sub (%esp), %eax
pop %eax
View
35 tinyboot1.tbf1
@@ -24,10 +24,9 @@ G — gets a byte from stdin
W — given an address and size on the stack, writes the specified number of
bytes to stdout
Q — exits the program with a return code of 0
-+adds two numbers
+-subtracts two numbers
@ — fetches a word from memory
! — stores to a word in memory whose address is on top of the stack
-~ — bitwise-negates a word
; — returns from a function
< — a less-than test
m — not used directly since the interpreter doesn't implement it,
@@ -41,7 +40,7 @@ e — the location of the entry point word in the ELF header
o — the location of the word in the ELF program header that specifies the .ORG
S — the location of the word in the ELF program header that specifies filesize
$ — the start of the area where the program gets compiled
--subtraction
++addition
` — the end of the headers
Z — the end of the predefined blob of bytes to output, with slight changes
H — the point where the program is currently getting compiled
@@ -85,6 +84,7 @@ Directory of magic numbers used, other than 0 and 1:
the system call number of the `read` system call
4 — the size of a 32-bit word;
second byte of `sub [%esp], %eax`;
+ second byte of `sub %eax, [%esp]`;
the system call number of the `write` system call
7 — in p_flags, means PF_R | PF_W | PF_X, making the program memory readable,
writable, and executable;
@@ -99,13 +99,15 @@ Directory of magic numbers used, other than 0 and 1:
' ' ASCII space character
33 — '!' ASCII exclamation mark character
35 — '#' ASCII number sign character
-36 — third byte of `sub [%esp], %eax`
+36 — third byte of `sub [%esp], %eax`;
+ third byte of `sub %eax, [%esp]`
40 — the size of any ELF segment headers that might exist;
'(' ASCII left parenthesis
-41 — ASCII right parenthesis
+41 — ASCII right parenthesis;
+ first byte of `sub %eax, [%esp]`
42 — '*' ASCII asterisk
-43 — first byte of `sub [%esp], %eax`;
- '+' ASCII plus
+43 — first byte of `sub [%esp], %eax`; maybe redundant
+45 — '-' ASCII minus
48 — '0' ASCII zero
52 — the size of the ELF header
57 — '9' ASCII nine
@@ -135,7 +137,6 @@ Directory of magic numbers used, other than 0 and 1:
118 — 'v' ASCII lowercase V
123 — '{' ASCII left curly brace
125 — '}' ASCII right curly brace
-126 — '~' ASCII tilde
127 — the first byte of an ELF executable
128 — the interrupt number Linux uses for system calls: 0x80
129 — first byte of `sub $xxx, %ebp`
@@ -151,14 +152,12 @@ Directory of magic numbers used, other than 0 and 1:
third byte of `setle %al`;
third byte of `movsbl %al, %eax`
195 — `ret` instruction
-200 — second byte of `dec %al` and `add %ecx, %eax`
+200 — second byte of `dec %al`
205 — `int` interrupt instruction
-208 — second byte of `not %eax`
229 — second byte of `mov %esp, %ebp`
232 — `call` instruction opcode, 4-byte immediate PC-relative destination
236 — second byte of `xchg %ebp, %esp`
237 — second byte of `sub $xxx, %ebp`
-247 — first byte of `not %eax`
254 — first byte of `dec %al`
1024 — a table size in bytes: 256 entries of 4 bytes each
4096 — the program runs with its origin at 4096, 0x1000, because that still
@@ -214,19 +213,18 @@ starting from `$`.
Then the only slightly hairy thing is to adjust `m` and `\`
to point to the right place: a bit before `$`. )
v Z ( the end of the non-compiled stuff. )
-( so the correct adjustment is to add $ - Z to them.
-)
-
-: $ h 8192 + ; ( this is where the program gets compiled )
-( hopefully 8192 is enough to skip over the compiler itself )
+( so the correct adjustment is to add $ - Z to them. )
v H # 0 ( "HERE", where we're compiling )
v X # 0 v Y # 0 ( temp vars )
: d X ! X @ X @ ; ( DUP ) : p X ! ; ( DROP/pop )
: x X ! Y ! X @ Y @ ; ( SWAP/exch )
-: - ~ 1 + + ; ( take two's complement, then add, to subtract )
+: + 0 x - x - 0 x - ; ( a + b is 0 - [[0 - a] - b] )
+: $ h 8192 + ; ( this is where the program gets compiled )
+( hopefully 8192 is enough to skip over the compiler itself )
+
: N [ 0 ; ] 1 ; ( Boolean negation ) : = - N ; ( equality )
: O [ p 1 ; ] ; ( Boolean OR ) : > x < ; ( greater-than )
@@ -296,8 +294,7 @@ v B # 0 ( buffer for w )
d 125 = [ p j ; ] ( } compiles a conditional jump to that address )
d 118 = [ p w V ; ] ( v registers the next non-blank as a
variable label )
- d 43 = [ p 89 . 1 . 200 . ; ] ( + is `pop %ecx; add %ecx, %eax` )
- d 126 = [ p 247 . 208 . ; ] ( ~ is `not %eax` )
+ d 45 = [ p 41 . 4 . 36 . 88 . ; ] ( - is `sub %eax, [%esp]; pop %eax` )
d 115 = [ p 89 . 136 . 8 . 88 . ; ] ( s is `pop %ecx; movb %cl, [%eax];
pop %eax` )
d 60 = [ p i ; ] ( < is implemented in “i” )

0 comments on commit a4d45f4

Please sign in to comment.