From 2e9d88ed765a0eb43a0adb32f9990f2ea5f42bf9 Mon Sep 17 00:00:00 2001 From: Kwarde Date: Sat, 5 Jul 2025 02:28:59 +0200 Subject: [PATCH] Adds print(), puts() --- src/console.asm | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/core.asm | 1 + src/tests.asm | 21 +++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/src/console.asm b/src/console.asm index 2e069bf..438352d 100644 --- a/src/console.asm +++ b/src/console.asm @@ -1 +1,47 @@ %include "src/constants.asm" + +extern strlen + +section .rodata + mNL db NL +section .text + global print + global puts + +;----- print(*str[]) -----; +; Prints given string to the console to stdout +; Return value: Amount of printed characters +; Used registers: +; rax* syscall >> (ret) amount of printed characters +; rdi* (arg) Pointer to str[] >> syscall arg (fd) +; rsi* syscall arg (pointer to str[]) +; rdx* syscall arg (length of str[]) +print: + sub rsp, SIZE_QWORD + mov rsi, rdi + call strlen + mov rdx, rax + mov rax, NR_write + mov rdi, FD_stdout + syscall + add rsp, SIZE_QWORD + ret + +;----- puts(*str[]) -----; +; Prints given string to the console to stdout and prints a new line +; Return value: Amount of printed characters +; Used registers: +; +puts: + sub rsp, SIZE_QWORD + call print + mov rsi, mNL + mov r10, rax + mov rax, NR_write + mov rdi, FD_stdout + mov rdx, 1 + syscall + mov rax, r10 + inc rax + add rsp, SIZE_QWORD + ret diff --git a/src/core.asm b/src/core.asm index a4e0a01..9911208 100644 --- a/src/core.asm +++ b/src/core.asm @@ -10,5 +10,6 @@ section .text ; rax* syscall ; rdi (arg) code to exit the program with exit: + sub rsp, SIZE_QWORD mov rax, NR_exit syscall diff --git a/src/tests.asm b/src/tests.asm index 6870e54..2240063 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -3,13 +3,22 @@ ; core.asm extern exit ; console.asm +extern print +extern puts ; string.asm extern strlen ; convert.asm section .rodata + TEST_print equ 1 TEST_strlen equ 1 + ; print() + msgPrint db "TEST print()",NL,EOS + + ; puts() + msgPuts db "TEST puts()",EOS + ; strlen() strlenStr1 db "Hello",EOS strlenStr2 db "Hello, world!",NL,EOS @@ -25,6 +34,18 @@ _start: mov rbp, rsp sub rsp, SIZE_QWORD +;--- +;--- print() +;--- + lea rdi, [rel msgPrint] + call print + +;--- +;--- puts() +;--- + lea rdi, [rel msgPuts] + call puts + ;--- ;--- strlen() ;---