Adds strlen()
This commit is contained in:
@@ -4,6 +4,11 @@ section .text
|
||||
global exit
|
||||
|
||||
;----- exit(exit_code) -----;
|
||||
; Exits the program with given exit code
|
||||
; Return value: N/A
|
||||
; Used registers:
|
||||
; rax* syscall
|
||||
; rdi (arg) code to exit the program with
|
||||
exit:
|
||||
mov rax, NR_exit
|
||||
syscall
|
||||
|
@@ -1 +1,21 @@
|
||||
%include "src/constants.asm"
|
||||
|
||||
section .text
|
||||
global strlen
|
||||
|
||||
;----- strlen(*str[]) -----;
|
||||
; Gets the length of given string
|
||||
; Return value: Length of given string
|
||||
; Used registers:
|
||||
; rax* Byte to check in str[] >> (ret) length of given string
|
||||
; rdi* (arg) Pointer to str[]
|
||||
; rcx* Counter for scasb
|
||||
strlen:
|
||||
xor rax, rax
|
||||
mov rcx, -1
|
||||
cld
|
||||
repne scasb
|
||||
mov rax, rcx
|
||||
not rax
|
||||
dec rax
|
||||
ret
|
||||
|
@@ -4,8 +4,20 @@
|
||||
extern exit
|
||||
; console.asm
|
||||
; string.asm
|
||||
extern strlen
|
||||
; convert.asm
|
||||
|
||||
section .rodata
|
||||
TEST_strlen equ 1
|
||||
|
||||
; strlen()
|
||||
strlenStr1 db "Hello",EOS
|
||||
strlenStr2 db "Hello, world!",NL,EOS
|
||||
|
||||
section .data
|
||||
|
||||
section .bss
|
||||
|
||||
section .text
|
||||
global _start
|
||||
_start:
|
||||
@@ -13,6 +25,19 @@ _start:
|
||||
mov rbp, rsp
|
||||
sub rsp, SIZE_QWORD
|
||||
|
||||
;---
|
||||
;--- strlen()
|
||||
;---
|
||||
; TEST 1
|
||||
lea rdi, [rel strlenStr1]
|
||||
call strlen
|
||||
; TEST 2
|
||||
lea rdi, [rel strlenStr2]
|
||||
call strlen
|
||||
|
||||
;---
|
||||
;--- exit()
|
||||
;---
|
||||
leave
|
||||
mov rdi, 0
|
||||
call exit
|
||||
|
Reference in New Issue
Block a user