Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22058

Using inline assembly in c to check for the largest value in a 32-bit integer array

$
0
0

I'm attempting to find the maximum element in an integer array using inline assembly in c. Below is my attempted solution with comments attached showing my logic.

My attempt:

static inline int max_elem()
{
  int list[] = {1,8,6,2,3};
  int max, temp;
  int length = 5;

 asm volatile(
  /* initialize max */
  "init:    mov (%1), %0;""         jmp start;

  /* increment arr to point to next int in memory (4-bytes) */
  "start:   add $4, %1;"

  /* check if you looped through all elements */       
  "         sub $1, %3;""         cmp $0, %3;""         je done;"

  /* compare max with current element in list */
  "         mov (%1), %2;""         cmp %2, %0;""         jl new_max;""new_max: mov %2, %0;""         jmp start;""done: ;"
  : "=a" (max) /* assign max to traditional return register */
  : "r" (list), "r" (temp), "r" (length) /* compiler picks the assignment of input variables */
 );
}

My attempted solution seems to give me a segmentation fault, despite my logic seeming correct to me. Any additional help or input would be appreciated, thanks!


Viewing all articles
Browse latest Browse all 22058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>