Last active
July 29, 2018 14:15
-
-
Save dnutiu/282c063824936f40b1156c9a57233d67 to your computer and use it in GitHub Desktop.
GCC Inline Assembly, str_set
This file contains 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 <stdio.h> | |
#include <stdlib.h> | |
int str_set(char * str, const size_t n, const size_t pos, const uint8_t val) { | |
uint8_t return_value = 0; | |
asm | |
( | |
"cmp %[pos], %[n]\n\t" | |
"movb $1, %[retv]\n\t" | |
"jle exit\n\t" | |
"movb %[val], 0(%[str], %[pos], 1)\n\t" | |
"movb $0, %[retv]\n\t" | |
"exit:\n\t" | |
: [str] "+r" (str), [retv] "=m" (return_value) // Out | |
: [pos] "r" ((uintptr_t)pos), [val] "r" (val), [n] "m" (n) // In | |
); | |
return return_value; | |
} | |
int main() | |
{ | |
char text[14] = "ASM Xs great!"; | |
uint8_t ret_val = 0; | |
ret_val = str_set(text, 13, 4, 'i'); | |
printf("Output: %s %d\n", text, ret_val); | |
ret_val = str_set(text, 13, 13, 'x'); | |
printf("Output: %s %d\n", text, ret_val); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment