stdio example added
This commit is contained in:
51
x86_64/1_stdio.asm
Normal file
51
x86_64/1_stdio.asm
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
global _start
|
||||||
|
|
||||||
|
section .data
|
||||||
|
str: db "Please give me a string: " ; \n = 0xA
|
||||||
|
|
||||||
|
strSize: equ $ - str
|
||||||
|
; $ = current assembler address.
|
||||||
|
; Since str is all that is above it must be the size
|
||||||
|
|
||||||
|
stdin: equ 0 ; POSIX
|
||||||
|
stdout: equ 1 ; POSIX
|
||||||
|
|
||||||
|
; Uninitialized space goes here, pretty much your heap of uninitialized bytes
|
||||||
|
; for all kinds of purposes
|
||||||
|
section .bss
|
||||||
|
strBuffer: resb 64 ; reserve 64 bytes
|
||||||
|
strBufferSize: equ 64 ; same as above
|
||||||
|
|
||||||
|
; This is where the code goes. Text. Makes sense, right?
|
||||||
|
section .text
|
||||||
|
_start:
|
||||||
|
; x64 has the syscall function, in contrast to int in x86 and ARM
|
||||||
|
|
||||||
|
; Write to stdout
|
||||||
|
mov rax, 1
|
||||||
|
mov rdi, stdout ; fd to write to
|
||||||
|
mov rsi, str ; pointer
|
||||||
|
mov rdx, strSize ; size
|
||||||
|
syscall ; all arguments are specified above! (x86 = int 0x80)
|
||||||
|
|
||||||
|
; Read from stdin
|
||||||
|
xor rax, rax ; 0
|
||||||
|
mov rdi, stdin ; fd to read from
|
||||||
|
mov rsi, strBuffer ; where to store it
|
||||||
|
mov rdx, strBufferSize ; maximum number of bytes
|
||||||
|
syscall
|
||||||
|
|
||||||
|
; syscall return codes are always stored in register A
|
||||||
|
; The number of bytes read in was returned through this
|
||||||
|
mov rdx, rax
|
||||||
|
|
||||||
|
mov rax, 1
|
||||||
|
mov rdi, stdout
|
||||||
|
mov rsi, strBuffer
|
||||||
|
syscall
|
||||||
|
|
||||||
|
|
||||||
|
; Exit routine
|
||||||
|
mov rax, 60 ; 60 is the exit routine
|
||||||
|
xor rdi, rdi ; our return code (0)
|
||||||
|
syscall
|
||||||
@@ -2,13 +2,19 @@ CC = gcc
|
|||||||
AS = nasm
|
AS = nasm
|
||||||
LD = ld
|
LD = ld
|
||||||
|
|
||||||
|
ASSEMBLIES = $(wildcard *.asm)
|
||||||
|
PROGRAMS = $(patsubst %.asm, bin/%, $(ASSEMBLIES))
|
||||||
|
|
||||||
.PHONY: all clean
|
.PHONY: all clean
|
||||||
|
|
||||||
all: build 0_basic
|
all: bin build $(PROGRAMS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
-rm -r build
|
-rm -r build
|
||||||
-rm 0_basic
|
-rm -r bin
|
||||||
|
|
||||||
|
bin:
|
||||||
|
mkdir bin
|
||||||
|
|
||||||
build:
|
build:
|
||||||
mkdir build
|
mkdir build
|
||||||
@@ -16,6 +22,5 @@ build:
|
|||||||
build/%.o: %.asm
|
build/%.o: %.asm
|
||||||
$(AS) -g -f elf64 $< -o $@
|
$(AS) -g -f elf64 $< -o $@
|
||||||
|
|
||||||
0_basic: build/0_basic.o
|
bin/%: build/%.o
|
||||||
$(LD) $< -o $@
|
$(LD) $< -o $@
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user