GithubHelp home page GithubHelp logo

ecoshub / nasm_c_source Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 0.0 160 KB

standard c functions with nasm assembly (x86_64)

Assembly 93.67% Shell 4.72% C 0.37% JavaScript 1.23%
netwide assembly assembly-x86

nasm_c_source's Introduction

Standard C Libraries in NASM

This project aims to recreate some of the Standard C Libraries using the Netwide Assembler (NASM). It was created as a learning exercise to help me understand the basics of assembly language, and to inspire others who are interested in learning NASM or any other assembly language.

Folder Structure

Folder Description
source Contains the source code for the project, including subfolders for standard and extended C Library files, as well as new procedures designed by the author.
source/standard Contains standard C Library files.
source/extended Contains extended C Library files. These procedures have a '_x' suffix and have additional features.
source/new Contains new procedures designed by the author. There are some useful procedures in this folder.
examples Contains examples of how to use some of the libraries.
tools Contains tools used to build the system. The lib_path in basm.bash and makelib.bash can be modified to change the default location.
lib Contains all the object files.
learning Contains notes about NASM syntax and definitions.
workspace Contains simple programs such as argument reading and echo, as well as the early stages of the author's learning process.

Usage

To use a .asm file in your C code, follow these steps:

  1. Compile it with NASM (32-bit compilation is recommended):
    nasm -f elf32 <as_file.asm> -o <out_name.o>
  1. Link it to your C code using GCC:
    gcc -m32 <your_c_file.c> -o <executable_name> <out_name.o>
  1. Run the executable:
    ./<executable_name>

To use the libraries in your own projects, you can either include the appropriate .asm files in your source code and assemble and link them as shown in the example above, or you can use the object files in the ./lib folder.

To use an object file, include the corresponding header file in your C code and link the object file when compiling.

For example, to use the strlen function from the string.h header file, you can do the following:

strlen.asm:

    section .text
        global strlen

    strlen:
        push ebp
        mov ebp, esp
        ; std prologue

        mov ecx, [ebp + 8]        ; reading string pointer from stack.
        xor eax, eax              ; reset eax to zero. now on it is gonna keep the char count.
        jmp compare               ; jump to comparison line

    loop:
        inc eax                   ; inc counter by one
    compare:
        cmp byte [ecx + eax], 0   ; dereference string pointer + exa to get value of char.
        jne loop                  ; jump to loop if its not a null terminator (0)

        ; std epilogue
        mov esp, ebp
        pop ebp
        ret

source.c:

    #include <stdio.h>

    int strlen(char *);

    void main() {
        char *string = "hello world";
        int string_length = strlen(string);
        printf("string_length: %i\n", string_length);
    }

nasm_c_source's People

Contributors

alicanerdurmaz avatar ecoshub avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.