Last active
April 19, 2023 17:52
-
-
Save Scherso/adc8966777941c3ed2617d51c9cf7000 to your computer and use it in GitHub Desktop.
‘Hello, World!’ C program made without the use of C's Standard Library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define WRITE 1 | |
#define STDOUT 1 | |
#define EXIT 60 | |
#define EXIT_SUCCESS 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "headers.h" | |
typedef unsigned long int size_t; // 64-bit, Absence of an extra 's' infers an unsigned value. | |
typedef long int ssize_t; // 64-bit, Extra 's' infers a signed value. | |
extern _Noreturn void exit(int code); | |
extern size_t strlen(const char *s); | |
extern ssize_t write(int fd, void const* data, size_t nbytes); | |
void _start(void) | |
{ | |
const char *msg = "Hello, World!\n"; | |
write(STDOUT, msg, strlen(msg)); | |
exit(EXIT_SUCCESS); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "headers.h" | |
.globl write, exit, strlen | |
.text | |
write: | |
mov $WRITE, %rax | |
syscall | |
ret | |
strlen: | |
xor %rax, %rax /* 'rax' register will serve as our return value. */ | |
.loop: | |
cmpb $0, (%rdi) /* Comparing the byte value's stored in 'rdi' to '0', with '0' being the ASCII null terminator. */ | |
je .end /* If we meet an ASCII null term, jump to '.end' label. */ | |
inc %rdi /* Incrementing the index of our character array. */ | |
inc %rax /* Incrementing our return value in correlation. */ | |
jmp .loop /* Looping back by jumping to our '.loop' label. */ | |
.end: | |
ret /* Returns the value stored in the 'rax' register. */ | |
exit: | |
mov $EXIT, %rax | |
syscall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment