This commit is contained in:
Christoph J. Scherr 2025-08-05 13:51:59 +02:00
parent 8994002299
commit eec2c5ea19
Signed by: PlexSheep
GPG key ID: 9EB784BB202BB7BB
4 changed files with 128 additions and 2 deletions

8
scripts/asm Executable file
View file

@ -0,0 +1,8 @@
#!/bin/sh
set -e
bash ./scripts/prepare.sh
nasm -f elf64 -o build/fb.o src/fb.asm
gcc -no-pie -o bin/fb_asm build/fb.o -lc
bin/fb_asm

3
scripts/prepare.sh Executable file
View file

@ -0,0 +1,3 @@
#!/bin/bash
mkdir -p bin build

115
src/fb.asm Normal file
View file

@ -0,0 +1,115 @@
; use the build-linux.sh script to assemble and link this
bits 64
section .note.GNU-stack
section .text
default rel; idk what this means?
global main
extern printf
extern exit
align 16
%macro check 2
mov rax, [rsp]
mov rbx, %1
mov rdx, 0
div rbx; dividend is rax. rdx has remainder and rax has the quotient
cmp rdx, 0
je %2
%endmacro
main:
enter 16, 0
mov rcx, 1; iterator
mov rbx, 0; temp var
mov rax, 0; temp var
push rcx; save iter on stack
.loop:
; show iteration
;mov rax, [rsp]; read iter from stack
;call piter
check 15, .hboth
check 3, .hfizz
check 5, .hbuzz
; else
jmp .hpnum
.hboth:
call fizz
call buzz
call nl
jmp .loopend
.hfizz:
call fizz
call nl
jmp .loopend
.hbuzz:
call buzz
call nl
jmp .loopend
.hpnum:
mov rax, [rsp]
call pnum
jmp .loopend
.loopend:
pop rcx
inc rcx
push rcx
cmp rcx, 100
jle .loop
; end of loop
.end:
leave
call exit
fizz:
mov rdi, Lfizz
call printf
ret
buzz:
mov rdi, Lbuzz
call printf
ret
nl:
mov rdi, Lnl
call printf
ret
pnum:
; see https: // www.mourtada.se/calling-printf-from-the-c-standard-library-in-assembly/
mov rdi, Lnumf
mov rsi, rax
mov rax, 0
call printf
ret
section .rodata
Lfizz:
db "Fizz", 0
.len equ $ - Lfizz
Lbuzz:
db "Buzz", 0
.len equ $ - Lbuzz
Lnl:
db 0x0a, 0
.len equ $ - Lnl
Lnumf:
db "%d", 10, 0
.len equ $ - Lnumf

View file

@ -1,9 +1,9 @@
for i in range(1,101):
if i % 15 == 0 :
print("FizzBuzz")
elif i % 5 == 0 :
print("Fizz")
elif i % 3 == 0 :
print("Fizz")
elif i % 5 == 0 :
print("Buzz")
else:
print(i)