Skip to content

Instantly share code, notes, and snippets.

@thecoder08
Created June 4, 2024 03:34
Show Gist options
  • Save thecoder08/24967c00ff883062bbf08ec95d316a37 to your computer and use it in GitHub Desktop.
Save thecoder08/24967c00ff883062bbf08ec95d316a37 to your computer and use it in GitHub Desktop.
x86-64 assembly bouncing ball demo using libxgfx
; x86_64 assembly libxgfx bouncing ball demo
; assemble with: nasm -f elf64 ball.asm
; link with: ld ball.o -lxgfx --dynamic-linker /lib64/ld-linux-x86-64.so.2 -o ball (needs libxgfx installed)
; run with: ./ball
global _start
extern initWindow
extern clear
extern circle
extern updateWindow
extern checkWindowEvent
section .text
_start: ; int main() {
mov rdi, 640
mov rsi, 480
mov rdx, title
call initWindow ; initWindow(640, 480, "Test Window");
mainloop: ; while(1) {
handleevent: ; Event event;
mov rdi, event
call checkWindowEvent ; while (checkWindowEvent(&event)) {
cmp rax, 0
je doneevents
mov eax, [event]
cmp eax, 4 ; if (event.type == WINDOW_CLOSE) {
jne handleevent
mov rax, 60
mov rdi, 0
syscall ; return 0;
doneevents: ; } }
mov rax, [xPos]
cmp rax, 640
jne notRight
mov QWORD [xDir], -1
notRight: ; if (xPos == 640) { xDir = -1; }
cmp rax, 0
jne notLeft
mov QWORD [xDir], 1
notLeft: ; if (xPos == 0) { xDir = 1; }
mov rax, [yPos]
cmp rax, 480
jne notDown
mov QWORD [yDir], -1
notDown: ; if (yPos == 480) { yDir = -1; }
cmp rax, 0
jne notUp
mov QWORD [yDir], 1
notUp: ; if (yPos == 0) { yDir = 1; }
mov rax, [xPos]
add rax, [xDir]
mov [xPos], rax ; xPos += xDir;
mov rax, [yPos]
add rax, [yDir]
mov [yPos], rax ; yPos += yDir;
call clear ; clear()
mov rdi, [xPos]
mov rsi, [yPos]
mov rdx, 10
mov rcx, 0xffff0000
call circle ; circle(xPos, yPos, 10, 0xffff0000);
call updateWindow ; updateWindow();
jmp mainloop ; } }
section .rodata
title: db "Test Window", 0
section .data
xPos: dq 100
xDir: dq 1
yPos: dq 100
yDir: dq 1
section .bss
event: resb 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment