{"id":13480230,"url":"https://github.com/xorvoid/sectorc","last_synced_at":"2025-04-07T23:12:57.045Z","repository":{"id":169189956,"uuid":"645052090","full_name":"xorvoid/sectorc","owner":"xorvoid","description":"A C Compiler that fits in the 512 byte boot sector of an x86 machine","archived":false,"fork":false,"pushed_at":"2024-06-11T17:44:11.000Z","size":44,"stargazers_count":1644,"open_issues_count":14,"forks_count":69,"subscribers_count":19,"default_branch":"main","last_synced_at":"2025-03-31T22:24:21.693Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xorvoid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-24T20:20:16.000Z","updated_at":"2025-03-25T21:43:47.000Z","dependencies_parsed_at":"2024-06-12T18:00:35.920Z","dependency_job_id":null,"html_url":"https://github.com/xorvoid/sectorc","commit_stats":null,"previous_names":["xorvoid/sectorc"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xorvoid%2Fsectorc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xorvoid%2Fsectorc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xorvoid%2Fsectorc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xorvoid%2Fsectorc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xorvoid","download_url":"https://codeload.github.com/xorvoid/sectorc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247744335,"owners_count":20988783,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T17:00:36.077Z","updated_at":"2025-04-07T23:12:57.024Z","avatar_url":"https://github.com/xorvoid.png","language":"C","readme":"# SectorC\nSectorC is a C compiler written in x86-16 assembly that fits within the 512 byte boot sector of an x86 machine. It supports a\nsubset of C that is large enough to write real and interesting programs. It is quite likely the smallest C compiler ever written.\n\nIn a base64 encoding, it looks like this:\n\n```\n6gUAwAdoADAfaAAgBzH/6DABPfQYdQXoJQHr8+gjAVOJP+gSALDDqluB+9lQdeAG/zdoAEAfy+gI\nAegFAYnYg/hNdFuE9nQNsOiqiwcp+IPoAqvr4j3/FXUG6OUAquvXPVgYdQXoJgDrGj0C2nUGV+gb\nAOsF6CgA68Ow6apYKfiD6AKrifgp8CaJRP7rrOg4ALiFwKu4D4Srq1fonP9ewz2N/HUV6JoA6BkA\nieu4iQRQuIs26IAAWKvD6AcAieu4iQbrc4nd6HkA6HYA6DgAHg4fvq8Bra052HQGhcB19h/DrVCw\nUKroWQDoGwC4WZGrW4D/wHUMuDnIq7i4AKu4AA+ridirH8M9jfx1COgzALiLBOucg/j4dQXorf/r\nJIP49nUI6BwAuI0G6wyE0nQFsLiq6wa4iwarAduJ2KvrA+gAAOhLADwgfvkx2zHJPDkPnsI8IH4S\nweEIiMFr2wqD6DABw+gqAOvqicg9Ly90Dj0qL3QSPSkoD5TGidjD6BAAPAp1+eu86Ln/g/jDdfjr\nslIx9osEMQQ8O3QUuAACMdLNFIDkgHX0PDt1BIkEMcBaw/v/A8H9/yvB+v/34fb/I8FMAAvBLgAz\nwYQA0+CaANP4jwCUwHf/lcAMAJzADgCfwIUAnsCZAJ3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAVao=\n```\n\n## Supported language\n\nA fairly large subset is supported: global variables, functions, if statements, while statements, lots of operators, pointer dereference, inline machine-code, comments, etc.\nAll of these features make it quite capable.\n\nFor example, the following program animates a moving sine-wave:\n\n```\nint y;\nint x;\nint x_0;\nvoid sin_positive_approx()\n{\n  y = ( x_0 * ( 157 - x_0 ) ) \u003e\u003e 7;\n}\nvoid sin()\n{\n  x_0 = x;\n  while( x_0 \u003e 314 ){\n    x_0 = x_0 - 314;\n  }\n  if( x_0 \u003c= 157 ){\n    sin_positive_approx();\n  }\n  if( x_0 \u003e 157 ){\n    x_0 = x_0 - 157;\n    sin_positive_approx();\n    y = 0 - y;\n  }\n  y = 100 + y;\n}\n\n\nint offset;\nint x_end;\nvoid draw_sine_wave()\n{\n  x = offset;\n  x_end = x + 314;\n  while( x \u003c= x_end ){\n    sin();\n    pixel_x = x - offset;\n    pixel_y = y;\n    vga_set_pixel();\n    x = x + 1;\n  }\n}\n\nint v_1;\nint v_2;\nvoid delay()\n{\n  v_1 = 0;\n  while( v_1 \u003c 50 ){\n    v_2 = 0;\n    while( v_2 \u003c 10000 ){\n      v_2 = v_2 + 1;\n    }\n    v_1 = v_1 + 1;\n  }\n}\n\nvoid main()\n{\n  vga_init();\n\n  offset = 0;\n  while( 1 ){\n    vga_clear();\n    draw_sine_wave();\n\n    delay();\n    offset = offset + 1;\n    if( offset \u003e= 314 ){ // mod the value to avoid 2^16 integer overflow\n      offset = offset - 314;\n    }\n  }\n}\n```\n\n### Screenshot\n\n![Moving Sinwave](img/sinwave.png)\n\n## Provided Example Code\n\nA few examples are provided that leverage the unique hardware aspects of the x86-16 IBM PC:\n- `examples/hello.c:` Print a text greeting on the screen writing to memory at 0xB8000\n- `examples/sinwave.c:` Draw a moving sine wave animation with VGA Mode 0x13 using an appropriately bad approximation of sin(x)\n- `examples/twinkle.c:` Play “Twinkle Twinkle Little Star” through the PC Speaker (Warning: LOUD)\n\n## Grammar\n  \nThe following grammar is accepted and compiled by sectorc:\n\n```\nprogram     = (var_decl | func_decl)+\nvar_decl    = \"int\" identifier \";\"\nfunc_decl   = \"void\" func_name \"{\" statement* \"}\"\nfunc_name   = \u003cidentifier that ends in \"()\" with no space\u003e\nstatement   = \"if(\" expr \"){\" statement* \"}\"\n            | \"while(\" expr \"){\" statement* \"}\"\n            | \"asm\" integer \";\"\n            | func_name \";\"\n            | assign_expr \";\"\nassign_expr = deref? identifier \"=\" expr\nderef       = \"*(int*)\"\nexpr        = unary (op unary)?\nunary       = deref identifier\n            | \"\u0026\" identifier\n            | \"(\" expr \")\"\n            | identifier\n            | integer\nop          = \"+\" | \"-\" | \"\u0026\" | \"|\" | \"^\" | \"\u003c\u003c\" | \"\u003e\u003e\"\n            | \"==\" | \"!=\" | \"\u003c\" | \"\u003e\" | \"\u003c=\" | \"\u003e=\"\n```\n\nIn addition, both `// comment` and `/* multi-line comment */` styles are supported.\n\n(NOTE: This grammar is 704 bytes in ascii, 38% larger than its implementation!)\n\n## How?\n\nSee blog post: [SectorC: A C Compiler in 512 bytes](https://xorvoid.com/sectorc.html)\n\n## Why?\n\nIn 2020, cesarblum wrote a Forth that fits in a bootsector: ([sectorforth](https://github.com/cesarblum/sectorforth))\n\nIn 2021, jart et. al. wrote a Lisp that fits in the bootsector: ([sectorlisp](https://github.com/jart/sectorlisp))\n\nNaturally, C always needs to come and crash (literally) every low-level systems party regardless of whether it was even invited.\n\n## Running\n\nDependencies:\n  - `nasm` for assembling (I used v2.16.01)\n  - `qemu-system-i386` for emulating x86-16 (I used v8.0.0)\n\nBuild: `./build.sh`\n\nRun: `./run.sh your_source.c`\n\nNOTE: Tested only on a MacBook M1\n\n## What is this useful for?\n\nProbably Nothing.\n\nOr at least that's what I thought when starting out. But, I didn't think I'd get such a feature set. Now, I'd say that it **might** be\nuseful for someone that wants to explore x86-16 bios functions and machine model w/o having to learn lots of x86 assembly first. But, then again, you\nshould just use a proper C compiler and write a tiny bootloader to execute it.\n","funding_links":[],"categories":["C"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxorvoid%2Fsectorc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxorvoid%2Fsectorc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxorvoid%2Fsectorc/lists"}