Difference between revisions of "Stage0"

From bootstrapping
Jump to navigation Jump to search
(Created page with "== stage0 == This defines .hex language which is simply reading in hex octets and skipping comments. <pre> ## Done 48 c7 c7 00 00 00 00 # mov $0x0,%rdi 48 c7 c0 3c 00 00 00...")
 
Line 1: Line 1:
== stage0 ==
+
== hex0 ==
  
 
This defines .hex language which is simply reading in hex octets and skipping comments.
 
This defines .hex language which is simply reading in hex octets and skipping comments.
Line 13: Line 13:
  
 
This maps out an ELF file for linux which implements a compiler for hex (!).
 
This maps out an ELF file for linux which implements a compiler for hex (!).
 +
 +
== hex2 ==
 +
 +
hex2 extends that language with labels and pointers.
 +
 +
* @ - 16 bit relative address
 +
* $ - 16 bit absolute address
 +
* & - 32 bit absolute address (for pointers)
 +
 +
<pre>
 +
# ;; Set p->Next = p->Next->Next->Next
 +
18020000 # LOAD32 R0 R2 0 ; Get Next->Next->Next
 +
23010000 # STORE32 R0 R1 0 ; Set Next = Next->Next->Next
 +
:Identify_Macros_1
 +
18010000 # LOAD32 R0 R1 0 ; Get node->next
 +
A0300000 # CMPSKIPI.NE R0 0 ; If node->next is NULL
 +
3C00 @Identify_Macros_Done # JUMP @Identify_Macros_Done ; Be done
 +
# ;; Otherwise keep looping
 +
3C00 @Identify_Macros_0 # JUMP @Identify_Macros_0
 +
:Identify_Macros_Done
 +
# ;; Restore registers
 +
0902803F # POPR R3 R15
 +
0902802F # POPR R2 R15
 +
0902801F # POPR R1 R15
 +
0902800F # POPR R0 R15
 +
0D01001F # RET R15
 +
:Identify_Macros_string
 +
444546494E450000 # "DEFINE"
 +
</pre>
 +
 +
Example of .hex2 code from [http://git.savannah.nongnu.org/cgit/stage0.git/tree/stage1/M0-macro.hex2 M0-macro.hex2]
 +
 +
== line macros ==
 +
 +
A macro system is implemented in .hex2. Using a [http://git.savannah.nongnu.org/cgit/stage0.git/tree/High_level_prototypes/defs defs] file like this:
 +
 +
<pre>
 +
DEFINE LOADR 2E0
 +
DEFINE LOADR8 2E1
 +
DEFINE LOADRU8 2E2
 +
</pre>
 +
 +
you can now program with the nmemonics instead of raw hexadecimal codes. This creates a new ".s" assembly language which looks like this:
 +
 +
<pre>
 +
:Prompt_Loop
 +
LOADXU8 R0 R3 R4            ; Get a char
 +
CMPSKIPI.NE R0 0            ; If NULL
 +
JUMP @Prompt_Done          ; We reached the end
 +
FPUTC                      ; Write it to TTY
 +
ADDUI R3 R3 1              ; Move to next char
 +
JUMP @Prompt_Loop          ; And loop again
 +
</pre>
 +
 +
sample taken from [http://git.savannah.nongnu.org/cgit/stage0.git/tree/stage1/CAT.s CAT.s]

Revision as of 14:49, 20 May 2017

hex0

This defines .hex language which is simply reading in hex octets and skipping comments.

## Done
48 c7 c7 00 00 00 00 # mov $0x0,%rdi
48 c7 c0 3c 00 00 00 # mov $0x3c,%rax
0f 05                # syscall

Example of .hex code from hex0.hex

This maps out an ELF file for linux which implements a compiler for hex (!).

hex2

hex2 extends that language with labels and pointers.

  • @ - 16 bit relative address
  • $ - 16 bit absolute address
  • & - 32 bit absolute address (for pointers)
# ;; Set p->Next = p->Next->Next->Next
18020000	# LOAD32 R0 R2 0 ; Get Next->Next->Next
23010000	# STORE32 R0 R1 0 ; Set Next = Next->Next->Next
:Identify_Macros_1
18010000	# LOAD32 R0 R1 0 ; Get node->next
A0300000	# CMPSKIPI.NE R0 0 ; If node->next is NULL
3C00 @Identify_Macros_Done	# JUMP @Identify_Macros_Done ; Be done
# ;; Otherwise keep looping
3C00 @Identify_Macros_0	# JUMP @Identify_Macros_0
:Identify_Macros_Done
# ;; Restore registers
0902803F	# POPR R3 R15
0902802F	# POPR R2 R15
0902801F	# POPR R1 R15
0902800F	# POPR R0 R15
0D01001F	# RET R15
:Identify_Macros_string
444546494E450000	# "DEFINE"

Example of .hex2 code from M0-macro.hex2

line macros

A macro system is implemented in .hex2. Using a defs file like this:

DEFINE LOADR 2E0
DEFINE LOADR8 2E1
DEFINE LOADRU8 2E2

you can now program with the nmemonics instead of raw hexadecimal codes. This creates a new ".s" assembly language which looks like this:

:Prompt_Loop
	LOADXU8 R0 R3 R4            ; Get a char
	CMPSKIPI.NE R0 0            ; If NULL
	JUMP @Prompt_Done           ; We reached the end
	FPUTC                       ; Write it to TTY
	ADDUI R3 R3 1               ; Move to next char
	JUMP @Prompt_Loop           ; And loop again

sample taken from CAT.s