init
This commit is contained in:
commit
a183d55b69
5 changed files with 244 additions and 0 deletions
26
flake.lock
generated
Normal file
26
flake.lock
generated
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1750400657,
|
||||||
|
"narHash": "sha256-3vkjFnxCOP6vm5Pm13wC/Zy6/VYgei/I/2DWgW4RFeA=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "b2485d56967598da068b5a6946dadda8bfcbcd37",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"id": "nixpkgs",
|
||||||
|
"ref": "nixos-25.05",
|
||||||
|
"type": "indirect"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
11
flake.nix
Normal file
11
flake.nix
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "nixpkgs/nixos-25.05";
|
||||||
|
};
|
||||||
|
outputs = {nixpkgs, ...}: let
|
||||||
|
system = "x86_64-linux";
|
||||||
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
|
in {
|
||||||
|
packages.${system}.default = pkgs.callPackage ./package.nix {};
|
||||||
|
};
|
||||||
|
}
|
30
package.nix
Normal file
30
package.nix
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
{
|
||||||
|
lib,
|
||||||
|
stdenv,
|
||||||
|
nasm,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
stdenv.mkDerivation {
|
||||||
|
name = "asm-test";
|
||||||
|
src = ./src;
|
||||||
|
nativeBuildInputs = [
|
||||||
|
nasm
|
||||||
|
];
|
||||||
|
buildInputs = [
|
||||||
|
];
|
||||||
|
configurePhase = ''
|
||||||
|
'';
|
||||||
|
buildPhase = ''
|
||||||
|
nasm -f elf main.asm
|
||||||
|
ld -m elf_i386 main.o -o main
|
||||||
|
'';
|
||||||
|
installPhase = ''
|
||||||
|
mkdir -p $out/bin
|
||||||
|
cp main $out/bin/main
|
||||||
|
'';
|
||||||
|
|
||||||
|
meta = {
|
||||||
|
license = lib.licenses.wtfpl;
|
||||||
|
mainProgram = "main";
|
||||||
|
};
|
||||||
|
}
|
116
src/lib.asm
Normal file
116
src/lib.asm
Normal 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
61
src/main.asm
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue