{"id":18573490,"url":"https://github.com/wangyu-/mycc","last_synced_at":"2025-04-10T07:32:00.842Z","repository":{"id":43297783,"uuid":"336254546","full_name":"wangyu-/mycc","owner":"wangyu-","description":"A tiny C compiler of 2k lines of codes, generates pure x86 asm code for DOS/Win32/Linux 3 backends.","archived":false,"fork":false,"pushed_at":"2021-03-08T02:13:23.000Z","size":75,"stargazers_count":36,"open_issues_count":0,"forks_count":9,"subscribers_count":2,"default_branch":"multi-file","last_synced_at":"2025-03-24T18:11:19.609Z","etag":null,"topics":["tiny-compiler"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wangyu-.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-05T11:38:53.000Z","updated_at":"2025-03-20T21:30:27.000Z","dependencies_parsed_at":"2022-09-02T22:10:42.833Z","dependency_job_id":null,"html_url":"https://github.com/wangyu-/mycc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangyu-%2Fmycc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangyu-%2Fmycc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangyu-%2Fmycc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangyu-%2Fmycc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wangyu-","download_url":"https://codeload.github.com/wangyu-/mycc/tar.gz/refs/heads/multi-file","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248176412,"owners_count":21060062,"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":["tiny-compiler"],"created_at":"2024-11-06T23:09:45.727Z","updated_at":"2025-04-10T07:32:00.550Z","avatar_url":"https://github.com/wangyu-.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mycc\n\nA tiny C compiler of 2k lines of codes, generates pure x86 asm code for DOS/Win32/Linux 3 backends.\n\n![image](images/helloworld1.png)\n![image](images/helloworld2.png)\n![image](images/helloworld3.png)\n\nMycc supports a reasonable subset of the C language. Check [tests](https://github.com/wangyu-/mycc/tree/master/tests) and [examples](https://github.com/wangyu-/mycc/tree/master/examples) folder to get an idea of what mycc can compile. Below are some examples: \n\n[nqueen.c](https://github.com/wangyu-/mycc/blob/master/examples/nqueen.c) is a mycc-compilable program that finds a solution for the n queen puzzle:\n\n```\n Q . . . . . . .\n . . . . Q . . .\n . . . . . . . Q\n . . . . . Q . .\n . . Q . . . . .\n . . . . . . Q .\n . Q . . . . . .\n . . . Q . . . .\n```\n\n[sudoku.c](https://github.com/wangyu-/mycc/blob/master/examples/sudoku.c) is a mycc-compilable program that fills the sudoku game for you:\n```\ninput:                                       output:\n 1 . 3 | . . . | 5 . 9                       1 4 3 | 6 2 8 | 5 7 9\n . . 2 | 1 . 9 | 4 . .                       5 7 2 | 1 3 9 | 4 6 8\n . . . | 7 . 4 | . . .                       9 8 6 | 7 5 4 | 2 3 1\n-------+-------+------                      -------+-------+------\n 3 . . | 5 . 2 | . . 6                       3 9 1 | 5 4 2 | 7 8 6\n . 6 . | . . . | . 5 .                       4 6 8 | 9 1 7 | 3 5 2\n 7 . . | 8 . 3 | . . 4                       7 2 5 | 8 6 3 | 9 1 4\n-------+-------+------                      -------+-------+------\n . . . | 4 . 1 | . . .                       2 3 7 | 4 8 1 | 6 9 5\n . . 9 | 2 . 5 | 8 . .                       6 1 9 | 2 7 5 | 8 4 3\n 8 . 4 | . . . | 1 . 7                       8 5 4 | 3 9 6 | 1 2 7\n ````\n\n\nMycc supports the syntax of embeding asm codes into C, mycc compiled programs are able to do syscall directly on their own:\n\n```\nchar getc()\n{\n  char a;\n  a=0;\n  __asm\n  {\n        mov eax, 3      ;the syscall num of \"read()\" on linux \n        mov ebx, 0      ;read from stdin, 0 is the fd of stdin\n        mov ecx, a@getc      ;save the result to the address of variable \"a\" inside function \"getc\"\n        mov edx, 1      ;read 1 character\n        int 0x80\n  }\n  return a;\n}\n```\n\nMycc is shipped with a built-in library (check [lib](https://github.com/wangyu-/mycc/tree/master/lib) and [lib.platform](https://github.com/wangyu-/mycc/tree/master/lib.platform)), implemented in the language of mycc itself.  Becasue of this, unlike most other tiny compilers, mycc generates \"pure asm\" codes that runs independently, without the need of linking to external libraries (e.g. libc). \n\n# How to use\nmake sure you have `nasm` installed.\n##### 1 build mycc with make:\n```\nmake\n```\n##### 2a To compile for Linux backend:\n```\n./mycc \u003cexamples/nqueen.c elf32\nnasm -f elf32 output.asm\nld -m elf_i386 output.o -o nqueen.out\n```\nThen `nqueen.out` is the run-able binary on linux. \n\n##### 2b To compile for DOS backend:\n```\n./mycc \u003cexamples/nqueen.c bin\nnasm -f bin output.asm -o nqueen.exe\n```\nThen `nqueen.exe` is the run-able binary on DOS. \n\n##### 2c To compile for Win32 backend:\n```\n./mycc \u003cexamples/nqueen.c win32\nnasm -f win32 output.asm\nld -m i386pe output.obj kernel32.dll -o nqueen32.exe        #(you need a copy of kernel32.dll)\n```\nThen `nqueen32.exe` is the run-able binary on windows.\n\n###### Note: all commands above run under linux. \n\n##### 3. all done, copy the binaries to the corresponding platforms and run.\n\n![image](images/nqueen1.png) ![image](images/nqueen2.png) ![image](images/nqueen3.png)\n# Easy testing with helper script\n\n`run.sh` is a helper script for easy testing, to make full use of this script you need `dosbox` and `wine` installed.\n\nFor linux backend:\n```\n./mycc \u003cexamples/nqueen.c elf32\n./run.sh\n```\n\nFor dos backend:\n```\n./mycc \u003cexamples/nqueen.c bin\n./run.sh\n```\n###### (it runs the compiled program inside `dosbox`)\n\nFor windows backend:\n```\n./mycc \u003cexamples/nqueen.c win32\n./run.sh\n```\n###### (it runs the compiled program inside `wine`)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwangyu-%2Fmycc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwangyu-%2Fmycc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwangyu-%2Fmycc/lists"}