Skip to content

Instantly share code, notes, and snippets.

@UplinkCoder
Last active February 10, 2022 02:50
Show Gist options
  • Save UplinkCoder/eb0bc5b0a05657a170017c7e210fc5c4 to your computer and use it in GitHub Desktop.
Save UplinkCoder/eb0bc5b0a05657a170017c7e210fc5c4 to your computer and use it in GitHub Desktop.
int write(int fd, const char*, unsigned long long length);
#define stderr_out(str) \
write(2, str, sizeof(str))
int main(int argc, char* argv[]) {
if (argc != 2)
{
stderr_out(
"bin_print expects one argument exactly which is the number to convert in hex or dec\n");
return -1;
}
unsigned long long number = 0;
_Bool isHex = 0;
for(char* p = argv[1]; *p; p++)
{
const char c = *p;
if (!isHex)
{
number *= 10;
number += (c - '0');
}
else
{
number *= 16;
if (c >= 'a' && c <= 'f')
{
number += ((c - 'a') + 10);
} else if (c >= 'A' && c <= 'F')
{
number += ((c - 'A') + 10);
} else
{
number += (c - '0');
}
}
if (!isHex && p == argv[1] + 1)
{
isHex = (c == 'x');
if (isHex) number = 0;
}
}
char format_buffer[66];
{
int i = 65;
for(; i--;)
{
if (number)
{
format_buffer[i] = (number & 1 ? '1' : '0');
number >>= 1;
continue;
}
break;
}
format_buffer[65] = '\n';
write(1, format_buffer + i + 1, 65 - i);
}
return 0;
}
.globl write
// parameters are just passed through
write:
/* syscall write(int fd, const void *buf, size_t count) */
mov w8, #64 /* write is syscall #64 */
svc #0 /* invoke syscall */
ret
.globl _start
_start:
ldr w0, [sp]
add x1, sp, #8 // this 8 would be a 4 in 32bit mode
bl main
b exit
.globl exit
exit:
/* syscall exit(int status) */
mov w8, #93 /* exit is syscall #93 */
svc #0 /* invoke syscall */
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment