add: minilibc to the examples

This commit is contained in:
2025-01-16 13:38:22 +01:00
parent 2adaa9773a
commit 57aa7b3e13
9 changed files with 276 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
; memcpy
end_:
push a1
call free
push a2
call free
mov eax, 0
end
memcpy:
; a1 -> buffer1
; a2 -> buffer2
; a3 -> n
mov a5, a1
mov a6, a2
mov a4, 0
loop:
cmp a4, a3
je end_
mov (char)*a5, (char)*a6
add a5, 1
add a6, 1
add a4, 1
jmp loop
main:
mov a3, 5
push 10
call malloc
mov a1, eax ;save the ptr
push 10
call malloc
mov a2, eax ;save the ptr
jmp memcpy

View File

@@ -0,0 +1,34 @@
; memset
end_:
push a1
call free
mov eax, 0
end
memset:
; a1 -> buffer
; a2 -> size
; a3 -> value
sub a2, 1
mov a5, 0
mov a4, a1
loop:
cmp a5, a2
je end_
mov (char)*a4, (char)a3
add a4, 1
add a5, 1
jmp loop
main:
mov a2, 10
mov a3, 97 ; 'a'
push a2
call malloc
mov a1, eax ;save the ptr
jmp memset

View File

@@ -0,0 +1,44 @@
; strcasecmp
set str1 "hello, world !\0"
set str2 "hello, world !\0"
end_:
mov eax, 0
end
fail:
mov eax, a4
sub eax, a5
end
main:
mov a1, str1
mov a2, str2
mov a3, 0
loop:
cmp (char)*a1, 0
jne 2
cmp (char)*a2, 0
je end_
mov a4, (char)*a1
cmp a4, 64
jna 2
cmp a4, 90
ja 2
add a4, 32
mov a5, (char)*a2
cmp a5, 64
jna 2
cmp a5, 90
ja 2
add a5, 32
cmp a4, a5
jne fail
add a1, 1
add a2, 1
jmp loop

View File

@@ -0,0 +1,25 @@
; strchr
set str "hello, world !\0"
found:
mov a2, a1
push a2
call print
end_:
mov eax, a2
end
main:
mov a1, str
mov a2, 0
mov a3, 111 ; 'o'
loop:
cmp *a1, 0
je end_
cmp a3, (char)*a1
je found
add a1, 1
jmp loop

View File

@@ -0,0 +1,29 @@
; strcmp
set str1 "hello, world !\0"
set str2 "hello, world !\0"
end_:
mov eax, 0
end
fail:
mov eax, (char)*a1
sub eax, (char)*a2
end
main:
mov a1, str1
mov a2, str2
mov a3, 0
loop:
cmp (char)*a1, 0
jne 2
cmp (char)*a2, 0
je end_
cmp (char)*a1, (char)*a2
jne fail
add a1, 1
add a2, 1
jmp loop

View File

@@ -0,0 +1,18 @@
; strlen
set str "hello, world !\0"
end_:
mov eax, a2
end
main:
mov a1, str
mov a2, 0
loop:
cmp (char)*a1, 0
je end_
add a2, 1
add a1, 1
jmp loop

View File

@@ -0,0 +1,34 @@
; strncmp
set str1 "hello, world !\0"
set str2 "hella, world !\0"
end_:
mov eax, 0
end
fail:
mov eax, (char)*a1
sub eax, (char)*a2
end
main:
mov a1, str1
mov a2, str2
mov a4, 6 ;n
mov a5, 1
mov a3, 0
loop:
cmp a5, a4
je end_
cmp (char)*a1, 0
jne 2
cmp (char)*a2, 0
je end_
cmp (char)*a1, (char)*a2
jne fail
add a1, 1
add a2, 1
add a5, 1
jmp loop

View File

@@ -0,0 +1,26 @@
; strrchr
set str "hello, world !\0"
found:
mov a2, a1
ret
end_:
push a2
call print
mov eax, a2
end
main:
mov a1, str
mov a2, 0
mov a3, 111 ; 'o'
loop:
cmp (char)*a1, 0
je end_
cmp (char)a3, (char)*a1
je found
add a1, 1
jmp loop

View File

@@ -0,0 +1,26 @@
; strstr
set str1 "hello, world !\0"
set str2 "hello\0"
end_:
mov eax, a1
end
fail:
mov eax, 0
end
main:
mov a1, str1
mov a2, str2
mov a3, 0
loop:
cmp (char)*a2, 0
je end_
cmp (char)*a1, (char)*a2
jne fail
add a1, 1
add a2, 1
jmp loop