This commit is contained in:
mtgmonkey 2025-06-27 04:10:26 -04:00
commit a183d55b69
5 changed files with 244 additions and 0 deletions

116
src/lib.asm Normal file
View file

@ -0,0 +1,116 @@
; ***************************
; slen : String -> Int
; calculates length of string
; ***************************
slen:
push ebx
mov ebx, eax
slen_nextchar:
cmp byte [eax], 0
jz slen_finished
inc eax
jmp slen_nextchar
slen_finished:
sub eax, ebx
pop ebx
ret
; *********************
; clear_bss : BSS -> IO
; clears memory block
; *********************
clear_bss:
cmp byte [eax], 0
jz clear_bss_finished
mov [eax], byte 0
inc eax
jmp clear_bss
clear_bss_finished:
ret
; *******************************
; scmp : String -> String -> Bool
; compares 2 strings exactly
; *******************************
scmp:
push ecx
scmp_nextchar:
mov cl, byte [ebx]
cmp byte [eax], cl
jnz scmp_bad
cmp byte [eax], 0
jz scmp_good
inc eax
inc ebx
jmp scmp_nextchar
scmp_good:
mov eax, 0
jmp scmp_finished
scmp_bad:
mov eax, 1
scmp_finished:
pop ecx
ret
; *********************
; sprint : String -> IO
; prints string
; *********************
sprint:
push edx
push ecx
push ebx
push eax
call slen
mov edx, eax
pop eax
mov ecx, eax
mov ebx, 1
mov eax, 4
int 80h
pop ebx
pop ecx
pop edx
ret
; ***********************
; sprintln : String -> IO
; prints string + 0Ah
; ***********************
sprintln:
call sprint
push eax ; initial
mov eax, 0Ah
push eax ; linefeed
mov eax, esp ; esp is stack pointer
call sprint
pop eax ; linefeed
pop eax ; initial
ret
; ************
; exit : IO
; exit program
; ************
exit:
mov ebx, 0
mov eax, 1
int 80h
ret

61
src/main.asm Normal file
View file

@ -0,0 +1,61 @@
%include 'lib.asm'
SECTION .data
welcome_message db 'Welcome to this test program! It is experimental and very subject to change.', 0h
prompt db 'Enter a string to print: ', 0h
exit_str db 'exit', 0Ah, 0h
SECTION .bss
; resb ; 0001 byte
; resw ; 0010 word
; resd ; 0100 double word
; resq ; 1000 quad word
; rest ; 1010 ten bytes
sinput: resb 255 ; 255 bytes
SECTION .text
global _start
_start:
pop ecx ; number of arguments
pop eax ; dispose of first value, the exe
dec ecx
mov eax, welcome_message
call sprintln
next_arg:
cmp ecx, 0h
jz prompt_loop
pop eax
call sprintln
dec ecx
jmp next_arg
prompt_loop:
mov eax, prompt
call sprint
mov eax, sinput
call clear_bss
mov edx, 255 ; max bytes to read
mov ecx, sinput ; buffer
mov ebx, 0 ; (STDIN) where to read from
mov eax, 3 ; (SYS_READ)
int 80h
mov eax, sinput
mov ebx, exit_str
call scmp
cmp byte eax, 0
jz done
mov eax, sinput
call sprint
jmp prompt_loop
done:
call exit