Curry
  • Curry
How do I write a get_min code in assembly? The code has to get the minimum value in an array.
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
chestercat
  • chestercat
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
Curry
  • Curry
I'm doing this in raspberry pi's ARM.
anonymous
  • anonymous
Can you tell me what instructions you have avaiable?
anonymous
  • anonymous
Assembly languages differ

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

Curry
  • Curry
What do you mean by which instructions?
Curry
  • Curry
sorry, i am very new to assembly. :(
anonymous
  • anonymous
what operations can you do? Do you have like 'mov' or 'add' or stuff like that?
Curry
  • Curry
yes, all those are availabe.
Curry
  • Curry
THose are actually the ones we learned.
anonymous
  • anonymous
Do you have sub? What sort of jumps do you have?
anonymous
  • anonymous
Show me what you have so far.
Curry
  • Curry
I literally have nothing so far. I have no clue how to start.
anonymous
  • anonymous
Do you have something like 'jz'?
Curry
  • Curry
I just know how each of the arithmetic assembly operations work like add r0, r1, r2 mean r0 = r1 + r2. We learned every arithmetic operation. but that's about it. not really how to use it.
anonymous
  • anonymous
Let me think. First let us start with the C code.
anonymous
  • anonymous
Then convert it and optimize it.
anonymous
  • anonymous
You return the first and last, not the max and min.
anonymous
  • anonymous
But since it is sorted, the min is the first?
Curry
  • Curry
yes.
anonymous
  • anonymous
it's ARMv6, but i'm not sure how we can predict the memory layout of structs without some sort of compiler directive for packing, and without that i'm not sure how to access the elements in a safe way
anonymous
  • anonymous
How is `list` defined?
anonymous
  • anonymous
@wio: in the C code it's given as: typedef struct List { int *sortedList; int size; int maxSize; } list;
Curry
  • Curry
um, list is in a struct. one sec.
anonymous
  • anonymous
how big are addresses? 64-bits? do you know how the struct is aligned by the compiler? when you compile your C code add -S and paste the backend assembly code
anonymous
  • anonymous
the C code containing the struct and stuff
Curry
  • Curry
32 bit.
Curry
  • Curry
i compiled with gcc -o -S array_test.c arraysort.c
anonymous
  • anonymous
remove -o and make sure you're compiling this code targeting the ARMv6 for raspberry pi
Curry
  • Curry
how do I know if I'm targetting the ARMv6?
Curry
  • Curry
I have to leave now, but i'll ocme back and post the results of the compilation.
Curry
  • Curry
BUt thanks for all your help.
anonymous
  • anonymous
well, in pseudocode for get_max you want something like: move [r0 + 4] to A /* now A = ls->size */ add [r0] to A /* now A = ls->sortedList + ls->size = &ls->sortedList[ls->size] */ sub 1 from A /* A = &ls->sortedList[ls->size - 1] */ mov [r0] to r0 /* set return value to ls->sortedList[ls->size - 1] */
anonymous
  • anonymous
then for get_min it's somewhat simpler since you just want ls->sortedList[0]
Curry
  • Curry
how would i change it for min?
Curry
  • Curry
why would moving r0 + 4 to a mean ls-> size? wouldn't that just be the first 4 bytes? which is ls->sortedList[0]?
Curry
  • Curry
@oldrin.bataku
anonymous
  • anonymous
No, ls->sortedList is at r0 + 0, and ls->size is at r0 + (sizeof ls->sortedList). We expect that ls->sortedList, or any pointer for that matter, is 4 bytes in a 32 bit processor.
Curry
  • Curry
wait isn't sizeof(ls->sortedList) the amount i malloced?
Curry
  • Curry
or is it just the size of the int pointer?
anonymous
  • anonymous
actually my code is broken anyways, it should [4*r0+16] into A and then subtract 4 since each element of sortedList is 4 bytes
anonymous
  • anonymous
@Curry well ls->sortedList is just int * so it's just the size of a pointer (you said 4 bytes)
Curry
  • Curry
move [r0 + 0] to A /* now A = ls->sortedList[0] */
Curry
  • Curry
is that right ^^
anonymous
  • anonymous
er oops, it should move [r0+4] as before and then multiply that by 4 to get a byte offset from ls->sortedList
anonymous
  • anonymous
well, sortedList[0] is *(sortedList + 0). The actual address is sortedList + 0
anonymous
  • anonymous
@Curry no, that is A = ls->sortedList or &sortedList[0]
Curry
  • Curry
hmm, so for max val, the sudo code should be
anonymous
  • anonymous
ls is a pointer; dereferencing it gives us the value at *ls which is sortedList, a pointer to the array of the values
Curry
  • Curry
mov r0+4 to A add [r0] to 4*A /* now A = ls->sortedList + ls->size = &ls->sortedList[ls->size] */ sub 1 from A /* A = &ls->sortedList[ls->size - 1] */ mov [r0] to r0 /* set return value to ls->sortedList[ls->size - 1] */
Curry
  • Curry
so do i deference it twice?
anonymous
  • anonymous
well, move [r0+4] to A, sub 1 from A, then multiply A by 4, then add [r0] to A, and then lastly move [A] to r0
Curry
  • Curry
and for min, it'd be mov r0 to A, multiply A by 4, add r0 to A and move to r0?
anonymous
  • anonymous
r0+4 is a pointer to ls->size, so [r0+4] is ls->size, and then we subtract 1 to get ls->size - 1, and then we multiply by 4 to get a byte offset from the start of ls->sortedList, and then we add that to ls->sortedList, and then we dereference that pointer to get ls->sortedList[ls->size-1]
anonymous
  • anonymous
for min you would do something like: move [r0] to A move [A] to r0 which is simply dereferencing ls twice; *ls gives the sortedList pointer, and dereferencing that gives its first element sortedList[0]
anonymous
  • anonymous
honestly i would just compile the C code you had and then use -S to look at gcc's assembly output; it should show you exactly how to do what you need to do
Curry
  • Curry
OMG THANK YOU GUS SO MUCH. <3
Curry
  • Curry
@oldrin.bataku Hey, so i wrote the get max function in assembly. But how do I call it from my main? also, when I compile, do i just include it like i include all my other .c files?
Curry
  • Curry
@e.mccormick
e.mccormick
  • e.mccormick
I do not use asm or c. But here are some references that look promissing. https://courses.engr.illinois.edu/ece390/books/labmanual/c-prog-mixing.html http://www.nongnu.org/avr-libc/user-manual/group__asmdemo.html
anonymous
  • anonymous
you need to tell the C compiler than you have external symbols: extern int get_max_ARM(list *); extern int get_min_ARM(list *);
anonymous
  • anonymous
then link the C, ASM object files together
anonymous
  • anonymous
you should not be passing that assembly code to gcc; presumably you need to use a separate assembler (not GCC's as)
Curry
  • Curry
well i have a header file, that has it, is that the same thing as having external symbols?
anonymous
  • anonymous
well, by default the C compiler assumes the storage class of a declaration is extern, so that's fine
anonymous
  • anonymous
those errors are because you're using a non-ARM GCC toolchain to build the code; use the set-up for compilation you were instructed to use for ARM
Curry
  • Curry
oo! kk. and does my asm code look aprox right?
Curry
  • Curry
and you said, when i'm compiling the files, i don't need to include the ,s files?
Curry
  • Curry
so right now, i'm doing gcc -o array array_test.c arraysort.c. Do i need to add any other falgs ?
anonymous
  • anonymous
what did your professor say to use for writing code for your raspberry pi?
Curry
  • Curry
what do you mean? like what language?
anonymous
  • anonymous
no, what toolchain? you're using a build of gcc that does not target ARM
Curry
  • Curry
oh, i'm trying to ssh into my school's account, i didn't hook up my raspberry pi rn. But, i'm guessing i need to hook it up, and compile the file locally on that, and if it works, then submit that?
anonymous
  • anonymous
yeah, you need to build using a toolchain that targets ARM since the assembly code is made for that; presumably you can hookup your raspberry pi and it'll have an appropriate toolchain on it?
Curry
  • Curry
that's what I'm guessing to. Haven't really tried it. What exactly is a toolchain?
anonymous
  • anonymous
as in the GCC toolchain of utilities; you need the version that is made to target ARM devices, so connect your Raspberry Pi and build the code on there
Curry
  • Curry
I compiled my code. So in my main, my code is int x = get_max_ARM(ls); /*ls is already initialized and all the regular c functions work fine with it*/ in my header file, the function is defined as int get_max_ARM(list *ls); this function is also in my regular c file and is also defined as int get_max_ARM(list *ls); However when i compile it on my raspberry ppi, i get an error that says (text+0x58) undefined reference to 'get_max_ARM' ld returned 1 exti status
Curry
  • Curry
@oldrin.bataku
anonymous
  • anonymous
yeah, you need to assemble the files and link the object code from gcc and your assembler or better yet just gcc -o like you had before
anonymous
  • anonymous
er gcc -o array array_test.c arraysort.c whatever-else.as
Curry
  • Curry
can i not deference the first operand?
Curry
  • Curry
and should it be mov r1, [r0+4]? instead of r1 being the 2nd operand?
Curry
  • Curry
mul only takes one operand, but i don't understand how it'd know which register to take from AND how much to multiply by.
anonymous
  • anonymous
gas uses mov dst, src i believe
anonymous
  • anonymous
mul doesn't take only one operand; can you show your error?
anonymous
  • anonymous
as a variable outside a function argument, int[5] is not reduced to a mere pointer
anonymous
  • anonymous
and it's arrays are never lvalues in C so you can't assign to them like that
Curry
  • Curry
so if ar[] was passed in as a function argument, then it'd be ok to do that? Because then it's treated just as int *ar? And just to really clarify, when doing int ar[5] it's only treated like an array and not a int pointer? cause i thought doing int ar[5] is actually doing the following diagram. |dw:1437017086879:dw|
Curry
  • Curry
@oldrin.bataku
anonymous
  • anonymous
yes, ar will function as a pointer to the first element, but you cannot directly assign to it like a pointer since its type is 'int[5]' which has a fixed size that is *not* the size of an integer pointer
anonymous
  • anonymous
that being said, arrays are treated as pointers whenever passed to another function and when subjected to arithmetic -- but they can never be assigned to like pointers

Looking for something else?

Not the answer you are looking for? Search for more explanations.