Skip to content

Instantly share code, notes, and snippets.

@skeeto
Created October 3, 2025 16:18
Show Gist options
  • Save skeeto/6fbd176bc0b48aa553426c31b682d054 to your computer and use it in GitHub Desktop.
Save skeeto/6fbd176bc0b48aa553426c31b682d054 to your computer and use it in GitHub Desktop.
spinning cube in assembly
; Assemble:
; $ nasm -fwin32 cube.asm
; Link with Mingw-w64:
; $ cc -mwindows -o cube.exe cube.obj libglfw3.a -lglu32 -lopengl32
; Link with MSVC:
; $ link /subsystem:windows cube.obj glfw3.lib opengl32.lib glu32.lib user32.lib gdi32.lib shell32.lib
;
; This is free and unencumbered software released into the public domain.
bits 32
extern _glBegin@4
extern _glClear@4
extern _glColor3f@12
extern _glEnable@4
extern _glEnd@0
extern _glLoadIdentity@0
extern _glMatrixMode@4
extern _glRotatef@16
extern _glTranslatef@12
extern _glVertex3f@12
extern _glfwCreateWindow
extern _glfwDestroyWindow
extern _glfwGetTime
extern _glfwInit
extern _glfwMakeContextCurrent
extern _glfwPollEvents
extern _glfwSwapBuffers
extern _glfwTerminate
extern _glfwWindowShouldClose
extern _gluPerspective@32
[section .text]
global _WinMain@16
_WinMain@16:
lea ecx, [esp+4]
and esp, -16
push dword [ecx-4]
push ebp
mov ebp, esp
push edi
push esi
push ebx
push ecx
sub esp, 56
call _glfwInit
test eax, eax
je L2
mov dword [esp+8], LC0
mov dword [esp+4], 480
mov dword [esp], 640
and dword [esp+16], 0
and dword [esp+12], 0
call _glfwCreateWindow
xchg eax, edi
test edi, edi
jne L3
call _glfwTerminate
L2:
or eax, -1
jmp L1
L3:
mov [esp], edi
call _glfwMakeContextCurrent
mov dword [esp], 2929
call _glEnable@4
push esi
L5:
mov [esp], edi
call _glfwWindowShouldClose
test eax, eax
jne L12
call _glfwGetTime
fstp dword [ebp-28]
mov dword [esp], 16640
call _glClear@4
push eax
mov dword [esp], 5889
call _glMatrixMode@4
push edx
call _glLoadIdentity@0
fld dword [LC1]
mov dword [esp+16], -1717986918
fstp qword [esp+24]
fld dword [LC4]
mov dword [esp+20], 1069128089
mov dword [esp+8], 1431655765
mov dword [esp+12], 1073042773
fstp qword [esp]
call _gluPerspective@32
sub esp, 32
mov dword [esp], 5888
call _glMatrixMode@4
push ecx
call _glLoadIdentity@0
mov dword [esp+8], 0xc0400000
mov dword [esp+4], 0x00000000
mov dword [esp], 0x00000000
call _glTranslatef@12
sub esp, 12
mov dword [esp+12], 0x00000000
mov dword [esp+8], 0x3f800000
mov dword [esp+4], 0x3f800000
fld dword [ebp-28]
fmul dword [LC8]
fstp dword [esp]
call _glRotatef@16
sub esp, 16
mov dword [esp], 7
call _glBegin@4
push ebx
mov dword [esp+8], 0x00000000
mov dword [esp+4], 0x00000000
mov dword [esp], 0x3f800000
call _glColor3f@12
mov esi, [LC9]
mov ebx, [LC10]
sub esp, 12
mov [esp+8], esi
mov [esp+4], esi
mov [esp], ebx
call _glVertex3f@12
sub esp, 12
mov [esp+8], esi
mov [esp+4], ebx
mov [esp], ebx
call _glVertex3f@12
sub esp, 12
mov [esp+8], ebx
mov [esp+4], ebx
mov [esp], ebx
call _glVertex3f@12
sub esp, 12
mov [esp+8], ebx
mov [esp+4], esi
mov [esp], ebx
call _glVertex3f@12
sub esp, 12
mov dword [esp+8], 0x00000000
mov dword [esp+4], 0x3f800000
mov dword [esp], 0x00000000
call _glColor3f@12
sub esp, 12
mov [esp+8], ebx
mov [esp+4], esi
mov [esp], esi
call _glVertex3f@12
sub esp, 12
mov [esp+8], ebx
mov [esp+4], ebx
mov [esp], esi
call _glVertex3f@12
sub esp, 12
mov [esp+8], esi
mov [esp+4], ebx
mov [esp], esi
call _glVertex3f@12
sub esp, 12
mov [esp+8], esi
mov [esp+4], esi
mov [esp], esi
call _glVertex3f@12
sub esp, 12
mov dword [esp+8], 0x3f800000
mov dword [esp+4], 0x00000000
mov dword [esp], 0x00000000
call _glColor3f@12
sub esp, 12
mov [esp+8], esi
mov [esp+4], ebx
mov [esp], esi
call _glVertex3f@12
sub esp, 12
mov [esp+8], ebx
mov [esp+4], ebx
mov [esp], esi
call _glVertex3f@12
sub esp, 12
mov [esp+8], ebx
mov [esp+4], ebx
mov [esp], ebx
call _glVertex3f@12
sub esp, 12
mov [esp+8], esi
mov [esp+4], ebx
mov [esp], ebx
call _glVertex3f@12
sub esp, 12
mov dword [esp+8], 0x00000000
mov dword [esp+4], 0x3f800000
mov dword [esp], 0x3f800000
call _glColor3f@12
sub esp, 12
mov [esp+8], ebx
mov [esp+4], esi
mov [esp], esi
call _glVertex3f@12
sub esp, 12
mov [esp+8], esi
mov [esp+4], esi
mov [esp], esi
call _glVertex3f@12
sub esp, 12
mov [esp+8], esi
mov [esp+4], esi
mov [esp], ebx
call _glVertex3f@12
sub esp, 12
mov [esp+8], ebx
mov [esp+4], esi
mov [esp], ebx
call _glVertex3f@12
sub esp, 12
mov dword [esp+8], 0x3f800000
mov dword [esp+4], 0x00000000
mov dword [esp], 0x3f800000
call _glColor3f@12
sub esp, 12
mov dword [esp+8], ebx
mov dword [esp+4], esi
mov dword [esp], esi
call _glVertex3f@12
sub esp, 12
mov [esp+8], ebx
mov [esp+4], esi
mov [esp], ebx
call _glVertex3f@12
sub esp, 12
mov [esp+8], ebx
mov [esp+4], ebx
mov [esp], ebx
call _glVertex3f@12
sub esp, 12
mov [esp+8], ebx
mov [esp+4], ebx
mov [esp], esi
call _glVertex3f@12
sub esp, 12
mov dword [esp+8], 0x3f800000
mov dword [esp+4], 0x3f800000
mov dword [esp], 0x00000000
call _glColor3f@12
sub esp, 12
mov [esp+8], esi
mov [esp+4], esi
mov [esp], ebx
call _glVertex3f@12
sub esp, 12
mov [esp+8], esi
mov [esp+4], esi
mov [esp], esi
call _glVertex3f@12
sub esp, 12
mov [esp+8], esi
mov [esp+4], ebx
mov [esp], esi
call _glVertex3f@12
sub esp, 12
mov [esp+8], esi
mov [esp+4], ebx
mov [esp], ebx
call _glVertex3f@12
sub esp, 12
call _glEnd@0
mov [esp], edi
call _glfwSwapBuffers
call _glfwPollEvents
jmp L5
L12:
mov [esp], edi
call _glfwDestroyWindow
call _glfwTerminate
xor eax, eax
L1:
lea esp, [ebp-16]
pop ecx
pop ebx
pop esi
pop edi
pop ebp
lea esp, [ecx-4]
ret
[section .data]
LC0: db `spinning cube\0`
LC1: dd 1092616192
LC4: dd 1110704128
LC8: dd 1112014848
LC9: dd -1090519040
LC10: dd 1056964608
// This is free and unencumbered software released into the public domain.
#include <GL/glu.h>
#include <GLFW/glfw3.h>
int main(void)
{
if (!glfwInit()) return -1;
GLFWwindow *w = glfwCreateWindow(640, 480, "spinning cube", 0, 0);
if (!w){ glfwTerminate(); return -1; }
glfwMakeContextCurrent(w);
glEnable(GL_DEPTH_TEST);
while (!glfwWindowShouldClose(w))
{
float t = (float)glfwGetTime();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 640.0/480.0, 0.1, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-3);
glRotatef(t*50, 1,1,0);
glBegin(GL_QUADS);
glColor3f(1,0,0);
glVertex3f( .5,-.5,-.5); glVertex3f( .5,.5,-.5);
glVertex3f( .5,.5, .5); glVertex3f( .5,-.5, .5);
glColor3f(0,1,0);
glVertex3f(-.5,-.5, .5); glVertex3f(-.5,.5, .5);
glVertex3f(-.5,.5,-.5); glVertex3f(-.5,-.5,-.5);
glColor3f(0,0,1);
glVertex3f(-.5,.5,-.5); glVertex3f(-.5,.5, .5);
glVertex3f( .5,.5, .5); glVertex3f( .5,.5,-.5);
glColor3f(1,1,0);
glVertex3f(-.5,-.5, .5); glVertex3f(-.5,-.5,-.5);
glVertex3f( .5,-.5,-.5); glVertex3f( .5,-.5, .5);
glColor3f(1,0,1);
glVertex3f(-.5,-.5,.5); glVertex3f( .5,-.5,.5);
glVertex3f( .5, .5,.5); glVertex3f(-.5, .5,.5);
glColor3f(0,1,1);
glVertex3f( .5,-.5,-.5); glVertex3f(-.5,-.5,-.5);
glVertex3f(-.5, .5,-.5); glVertex3f( .5, .5,-.5);
glEnd();
glfwSwapBuffers(w);
glfwPollEvents();
}
glfwDestroyWindow(w);
glfwTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment