├── LICENSE ├── README.md ├── c2nasm.sh └── test ├── exec_test.sh └── hello.c /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # C2NASM 3 | 4 | Uses gcc and objconv to convert a C program to nasm. 5 | 6 | To use it, give execution permission to c2nasm.sh and then: 7 | 8 | ``` 9 | ./c2nasm.sh 10 | ``` 11 | 12 | The result will be a .nasm file with the nasm, and a .run file which is the executable. 13 | 14 | Note that you might get a better nasm output by tunning the optimization parameter in gcc (`-O0` for no optimization, `-O3` to optimize as much as possible). 15 | 16 | 17 | 18 | Based on the question: 19 | http://stackoverflow.com/questions/20737947/how-to-generate-a-nasm-compilable-assembly-code-from-c-source-code-on-linux/ 20 | -------------------------------------------------------------------------------- /c2nasm.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | help(){ 4 | echo "$1: Uses gcc and objconv to convert a C program to nasm" 5 | echo "usage: $1 " 6 | exit 0 7 | } 8 | [ -z "$1" ] || [ "$1" == "-h" ] && help "$(basename "$0")" 9 | 10 | C_FILE="$1" 11 | O_FILE="$C_FILE.o" 12 | NASM_FILE="$C_FILE.nasm" 13 | NASM_O_FILE="$NASM_FILE.o" 14 | EXEC_FILE="$C_FILE.run" 15 | gcc -m32 -c -o "$O_FILE" "$C_FILE" 16 | objconv -fnasm "$O_FILE" "$NASM_FILE" 17 | sed -i 's|st(0)|st0 |g' "$NASM_FILE" 18 | sed -i 's|noexecute| |g' "$NASM_FILE" 19 | sed -i 's|execute| |g' "$NASM_FILE" 20 | sed -i 's|: function||g' "$NASM_FILE" 21 | sed -i 's|?_|L_|g' "$NASM_FILE" 22 | sed -i -n '/SECTION .eh_frame/q;p' "$NASM_FILE" 23 | sed -i 's|;.*||g' "$NASM_FILE" 24 | sed -i 's/^M//g' "$NASM_FILE" 25 | sed -i 's|\s\+$||g' "$NASM_FILE" 26 | sed -i 's|align=1||g' "$NASM_FILE" 27 | echo 'Nasm file generated in '"$NASM_FILE" 28 | nasm -f elf32 -o "$NASM_O_FILE" "$NASM_FILE" 29 | gcc -m32 -lGL -lm -o "$EXEC_FILE" "$NASM_O_FILE" 30 | echo 'Successfully compiled '"$NASM_FILE" to "$EXEC_FILE" 31 | 32 | -------------------------------------------------------------------------------- /test/exec_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | 4 | test_clean(){ 5 | rm -f hello.c.nasm 6 | rm -f hello.c.nasm.o 7 | rm -f hello.c.o 8 | rm -f hello.c.run 9 | } 10 | 11 | assert_exists(){ 12 | [ -f "$1" ] || { 13 | echo "Test Failed: File $1 should exist" 14 | exit 15 | } 16 | } 17 | assert_return(){ 18 | "$1" 19 | ret="$?" 20 | [ "$ret" == "$2" ] || { 21 | echo "Test Failed: Return of $1 should be $2, instead I got $ret " 22 | exit 23 | } 24 | } 25 | test_clean 26 | 27 | ../c2nasm.sh hello.c 28 | assert_exists hello.c.nasm 29 | assert_exists hello.c.nasm.o 30 | assert_exists hello.c.o 31 | assert_exists hello.c.run 32 | assert_return ./hello.c.run 6 33 | 34 | echo "==================" 35 | echo All tests passed 36 | echo "==================" 37 | 38 | -------------------------------------------------------------------------------- /test/hello.c: -------------------------------------------------------------------------------- 1 | 2 | int main(){ 3 | char a =1; 4 | char b =2; 5 | a = b + a; 6 | a *= 2; 7 | return a; 8 | } 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | --------------------------------------------------------------------------------