Update 2024-07-27-sys-write-string.md
This commit is contained in:
parent
802eac1593
commit
5985e0b7b1
@ -26,7 +26,7 @@ The syntax looks like this:
|
|||||||
|
|
||||||
`0xa` is just the ASCII hex representation of a new line.
|
`0xa` is just the ASCII hex representation of a new line.
|
||||||
|
|
||||||
```asm
|
```
|
||||||
; sys_write_string.asm
|
; sys_write_string.asm
|
||||||
section .data
|
section .data
|
||||||
s1 db "s1", 0x0a, 0; terminated string with NL
|
s1 db "s1", 0x0a, 0; terminated string with NL
|
||||||
@ -38,7 +38,7 @@ Many standard libraries add the `NULL` terminator to initialized strings. This i
|
|||||||
|
|
||||||
Although, since `sys_write` does not expect a terminated string, this should be stripped. By taking the address of `s1` minus the terminated character (1 byte/8 bits).
|
Although, since `sys_write` does not expect a terminated string, this should be stripped. By taking the address of `s1` minus the terminated character (1 byte/8 bits).
|
||||||
|
|
||||||
```asm
|
```
|
||||||
s1Len equ $-s1-1; offset of s1 - terminator
|
s1Len equ $-s1-1; offset of s1 - terminator
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -46,7 +46,7 @@ s1Len equ $-s1-1; offset of s1 - terminator
|
|||||||
|
|
||||||
- `.bss` stands for _Block Started by Symbol_, and contains uninitialized variables that assign memory at runtime. For this example, it makes sense to define the value in the `.data` block above instead.
|
- `.bss` stands for _Block Started by Symbol_, and contains uninitialized variables that assign memory at runtime. For this example, it makes sense to define the value in the `.data` block above instead.
|
||||||
|
|
||||||
```asm
|
```
|
||||||
section .bss
|
section .bss
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ The final section required is to define the entrypoint of the program
|
|||||||
- `.text` refers to the code segment: the program's virtual address space that contains executable instructions.
|
- `.text` refers to the code segment: the program's virtual address space that contains executable instructions.
|
||||||
- `main` is our program's entrypoint.
|
- `main` is our program's entrypoint.
|
||||||
|
|
||||||
```asm
|
```
|
||||||
section .text
|
section .text
|
||||||
global main
|
global main
|
||||||
```
|
```
|
||||||
@ -66,7 +66,7 @@ global main
|
|||||||
|
|
||||||
If the `global` keyword was omitted, then the linker (`ld`) will not see it, since `main` is scoped to the object file:
|
If the `global` keyword was omitted, then the linker (`ld`) will not see it, since `main` is scoped to the object file:
|
||||||
|
|
||||||
```asm
|
```
|
||||||
[0x3bb@heimat 2]$ make
|
[0x3bb@heimat 2]$ make
|
||||||
nasm -f elf64 -g -F dwarf sys_write_string.asm -l sys_write_string.lst
|
nasm -f elf64 -g -F dwarf sys_write_string.asm -l sys_write_string.lst
|
||||||
sys_write_string.asm:9: warning: label alone on a line without a colon might be in error [-w+label-orphan]
|
sys_write_string.asm:9: warning: label alone on a line without a colon might be in error [-w+label-orphan]
|
||||||
@ -89,7 +89,7 @@ With the `global` directive set correctly, the [symbol type identifier](https://
|
|||||||
## label
|
## label
|
||||||
The `main` label serves as an alias to the block of instructions defined below
|
The `main` label serves as an alias to the block of instructions defined below
|
||||||
|
|
||||||
```asm
|
```
|
||||||
main:
|
main:
|
||||||
push rbp; push base address of stack frame to restore later
|
push rbp; push base address of stack frame to restore later
|
||||||
mov rbp, rsp; copy address of current stack frame
|
mov rbp, rsp; copy address of current stack frame
|
||||||
|
Loading…
Reference in New Issue
Block a user