hex1 assembly works

This commit is contained in:
janis 2026-06-05 02:42:44 +02:00
parent 7472eb1279
commit c2934ffd50
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8
2 changed files with 146 additions and 57 deletions

90
stages/hex0/helloworld.h1 Normal file
View file

@ -0,0 +1,90 @@
; ELF header
; e_ident: u128
; Magic number: 0x7f454c46 = "\x7fELF"
7f 45 4c 46
; EI_CLASS: u8 = ELFCLASS64
02
; EI_DATA: u8 = ELFDATA2LSB
01
; EI_VERSION: u8 = EV_CURRENT
01
; EI_OSABI: u8 = ELFOSABI_NONE
00
; EI_ABIVERSION: u8 = 0
00
; EI_PAD: [u8; 7]
00 00 00 00 00 00 00
; e_type: u16 = E_EXEC
02 00
; e_machine: u16 = EM_X86_64
3e 00
; e_version: u32 = EV_CURRENT
01 00 00 00
; e_entry: u64 = 0x400084
84 00 40 00 00 00 00 00
; e_phoff: u64 = 0x40
40 00 00 00 00 00 00 00
; e_shoff: u64 = 0x0
00 00 00 00 00 00 00 00
; e_flags: u32 = 0
00 00 00 00
; e_ehsize: u16 = 64
40 00
; e_phentsize: u16 = 56
38 00
; e_phnum: u16 = 1
01 00
; e_shentsize: u16 = 0
00 00
; e_shnum: u16 = 0
00 00
; e_shstrndx: u16 = 0
00 00
; End of ELF header
;
; Program header
; p_type: u32 = PT_LOAD
01 00 00 00
; p_flags: u32 = PF_R | PF_X
05 00 00 00
; p_offset: u64 = 0
00 00 00 00 00 00 00 00
; p_vaddr: u64 = 0x400000
00 00 40 00 00 00 00 00
; p_paddr: u64 = 0x0
00 00 00 00 00 00 00 00
; p_filesz: u64
ae 00 00 00 00 00 00 00
; p_memsz: u64
ae 00 00 00 00 00 00 00
; p_align: u64 = 0x1000
00 10 00 00 00 00 00 00
; End of program header
;
; Ehdr size = 64 = 0x40
; Phdr size = 56 = 0x38
; .text size = 54 = 0x36
; total 0xae
;
; Code segment
;
; _start
@hello_world:
68 65 6c 6c 6f 20 77 6f 72 6c 64 21
@_start:
; mov rax, 1
48 c7 c0 01 00 00 00
; mov rdi, 1
48 c7 c7 01 00 00 00
; mov rsi, [@hello_world]
48 8d 35 'hello_world
; mov rdx, 12
48 c7 c2 0c 00 00 00
; syscall
0f 05
; mov rax, 60 (sys_exit)
48 c7 c0 3c 00 00 00
; xor rdi, rdi
48 31 ff
; syscall
0f 05

View file

@ -199,59 +199,6 @@ get_next_token:
call skip_comment call skip_comment
jmp get_next_token jmp get_next_token
;; read the label into scratch, call push_lbl
label:
cmp al, '@'
jne .not_label
xor rsi, rsi ; rsi = length of label
.label_loop:
push rsi
call rd_buf
pop rsi
lea rdi, [r15 + 0x6004] ; rdi = pointer to scratch
cmp al, ':'
je .label_ret
mov byte [rdi + rsi], al
inc rsi
jmp .label_loop
.label_ret:
lea rdi, [r15 + 0x6004] ; rdi = pointer to scratch
call push_lbl
.not_label:
ret
label_ref:
cmp al, "'"
jne .not_label_ref
call rd_buf
xor rsi, rsi ; rsi = length of label
.label_ref_loop:
push rsi
call rd_buf
pop rsi
lea rdi, [r15 + 0x6004] ; rdi = pointer to scratch
cmp al, ' '
je .label_ref_ret
cmp al, `\t`
je .label_ref_ret
cmp al, `\n`
je .label_ref_ret
mov byte [rdi + rsi], al
inc rsi
jmp .label_ref_loop
.label_ref_ret:
lea rdi, [r15 + 0x6004]
call find_lbl
add dword [r15 + 0x500c], 4 ; advance the file offset by 4
sub eax, dword [r15 + 0x500c] ; add current file offset
mov edi, dword [r15 + 0x5004] ; get current write cursor
mov dword [r15 + 0x1000 + rdi], eax ; write the opcode for jmp rel32
add dword [r15 + 0x5004], 4 ; advance the write cursor by 4
.not_label_ref:
ret
char_to_u4: char_to_u4:
cmp al, '0' cmp al, '0'
jb .invalid_hex jb .invalid_hex
@ -296,6 +243,62 @@ octet:
add rsp, 8 add rsp, 8
ret ret
label_ref:
cmp al, "'"
jne .not_label_ref
xor rsi, rsi ; rsi = length of label
.label_ref_loop:
push rsi
call rd_buf
pop rsi
lea rdi, [r15 + 0x6004] ; rdi = pointer to scratch
cmp al, ' '
je .label_ref_ret
cmp al, `\t`
je .label_ref_ret
cmp al, `\n`
je .label_ref_ret
mov byte [rdi + rsi], al
inc rsi
jmp .label_ref_loop
.label_ref_ret:
lea rdi, [r15 + 0x6004]
call find_lbl
add dword [r15 + 0x500c], 4 ; advance the file offset by 4
sub eax, dword [r15 + 0x500c] ; add current file offset
mov edi, dword [r15 + 0x5004] ; get current write cursor
mov dword [r15 + 0x1000 + rdi], eax ; write the opcode for jmp rel32
add dword [r15 + 0x5004], 4 ; advance the write cursor by 4
ret
.not_label_ref:
call octet
ret
;; read the label into scratch, call push_lbl
label:
cmp al, '@'
jne .not_label
xor rsi, rsi ; rsi = length of label
.label_loop:
push rsi
call rd_buf
pop rsi
lea rdi, [r15 + 0x6004] ; rdi = pointer to scratch
cmp al, ':'
je .label_ret
mov byte [rdi + rsi], al
inc rsi
jmp .label_loop
.label_ret:
lea rdi, [r15 + 0x6004] ; rdi = pointer to scratch
call push_lbl
ret
.not_label:
call label_ref
ret
global _start global _start
_start: _start:
push rbp push rbp
@ -312,8 +315,4 @@ _start:
call rd_buf call rd_buf
call get_next_token call get_next_token
call label call label
call label_ref
call octet
jmp .loop jmp .loop