Skip to content

Instantly share code, notes, and snippets.

@demize
Created February 8, 2024 23:52
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 demize/9a0a63a15b9b4449400317d0231599ae to your computer and use it in GitHub Desktop.
Save demize/9a0a63a15b9b4449400317d0231599ae to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int32_t add_i32(int32_t a, int32_t b)
{
int32_t result = 0;
int overflow = 0;
asm volatile (
"mov %[a], %[result];"
"add %[b], %[result];"
: [overflow] "=@cco" (overflow), [result] "=r" (result)
: [a] "r" (a), [b] "r" (b)
: "cc"
);
if(overflow) {
printf("Calculation overflowed!\n");
exit(1);
}
return result;
}
int main(int argc, char **argv) {
if(argc != 3) {
printf("Usage: %s a b\n", argv[0]);
exit(0);
}
int a = strtol(argv[1], NULL, 10), b = strtol(argv[2], NULL, 10);
if(!a && !strcmp(argv[1], "0") || !b && !strcmp(argv[1], "0")) {
printf("Please provide two non-zero numbers to add\n");
}
int32_t res = add_i32(a, b);
printf("%d\n", res);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment