0

I'm new to Assembly so please bear with me! As the C program says I'm taking an array and the number of elements as parameters and passing the array and the number of elements to minmax. But my second task is to convert it to Assembly which has been a hassle.

#include <stdio.h>
void minmax(unsigned int ary[], int len)
{
    int min = len, max = 0;

    for(int i = 0; i < len; i++)
    {
        if(ary[i] < min)
        {
            min = ary[i];
        }
        if (ary[i] > max)
        {
            max = ary[i];
        }
    }
    printf("min: %d max: %d\n", min, max);
}

int main()
{
    static unsigned int ary[] = {4, 3, 2, 5, 6, 2, 3, 7, 8, 3};
    const unsigned int ary_size = sizeof(ary) / sizeof(unsigned int);
    minmax(ary, ary_size);
    return 0;
}

And here is my assembly conversion that I've tried. I can't seem to load and initialize my min max and let the min equal the len in assembly. Please point out some hints on these instructions

.begin 
.org 2048 
a_start .equ 3000 
array_size .equ 10 
or %r0, 0, %r1 
ld [len], %r1 
ld [min], %r2 
ld [max], %r3   
forloop: 
cmp %r1, array_size 
bge endforloop 

ld %r8, [a], %r2 

cmp %r3, min 
do next 

ld %r11, [a] 
move min, [a] 

cmp %r3, max 
do next 

ld %r11, [a] 
move max, [a] 

endforloop: 
halt 

.org a_start 
array: .dwb array_size 
a: 4,3,2,5,6,2,3,7,8,3 
.end

EDIT: Anyone help please? The assembly generated code isn't that helpful with all its scripts. I would prefer someone pointing out where I need to make changes for what reason. Many thanks!

  • You know there is a compiler switch that will generate the translated asm from the C code for you, right? – WhozCraig Oct 08 '14 at 18:28
  • No I don't know about that! How would I achieve that? Which tool do I use to convert it? – pointing_Stack Oct 08 '14 at 18:31
  • Your compiler is... ? If you google "<> generate assembly" you'll quickly find out how. For `clang` and `gcc` it is `-S`, for example. Start with **small** simple one-function source files to get the hang of how the code is generated (it isn't hard). – WhozCraig Oct 08 '14 at 18:34
  • Thanks for the recommendations. Thad definitely helps to test and analyze my code. On the other it would be great to get real help from here to see where and why I'm making errors. – pointing_Stack Oct 08 '14 at 18:47
  • you have small bug in your C code, `int min = len, max = 0;` should be `int min = max = ary[0];` – Iłya Bursov Oct 08 '14 at 19:31
  • Tested your suggestion and the loop seems to work properly. Thanks! – pointing_Stack Oct 08 '14 at 19:59

0 Answers0