Skip to content

Instantly share code, notes, and snippets.

@dnutiu
Last active July 29, 2018 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnutiu/282c063824936f40b1156c9a57233d67 to your computer and use it in GitHub Desktop.
Save dnutiu/282c063824936f40b1156c9a57233d67 to your computer and use it in GitHub Desktop.
GCC Inline Assembly, str_set
#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