Skip to content

Instantly share code, notes, and snippets.

@dnutiu
Last active Jul 29, 2018
Embed
What would you like to do?
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