Ask your own question, for FREE!
Computer Science 24 Online
OpenStudy (curry):

How do I write a get_min code in assembly? The code has to get the minimum value in an array.

OpenStudy (curry):

I'm doing this in raspberry pi's ARM.

OpenStudy (anonymous):

Can you tell me what instructions you have avaiable?

OpenStudy (anonymous):

Assembly languages differ

OpenStudy (curry):

What do you mean by which instructions?

OpenStudy (curry):

sorry, i am very new to assembly. :(

OpenStudy (anonymous):

what operations can you do? Do you have like 'mov' or 'add' or stuff like that?

OpenStudy (curry):

yes, all those are availabe.

OpenStudy (curry):

THose are actually the ones we learned.

OpenStudy (anonymous):

Do you have sub? What sort of jumps do you have?

OpenStudy (anonymous):

Show me what you have so far.

OpenStudy (curry):

I literally have nothing so far. I have no clue how to start.

OpenStudy (anonymous):

Do you have something like 'jz'?

OpenStudy (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.

OpenStudy (anonymous):

Let me think. First let us start with the C code.

OpenStudy (anonymous):

Then convert it and optimize it.

OpenStudy (anonymous):

You return the first and last, not the max and min.

OpenStudy (anonymous):

But since it is sorted, the min is the first?

OpenStudy (curry):

yes.

OpenStudy (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

OpenStudy (anonymous):

How is `list` defined?

OpenStudy (anonymous):

@wio: in the C code it's given as: typedef struct List { int *sortedList; int size; int maxSize; } list;

OpenStudy (curry):

um, list is in a struct. one sec.

OpenStudy (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

OpenStudy (anonymous):

the C code containing the struct and stuff

OpenStudy (curry):

32 bit.

OpenStudy (curry):

i compiled with gcc -o -S array_test.c arraysort.c

OpenStudy (anonymous):

remove -o and make sure you're compiling this code targeting the ARMv6 for raspberry pi

OpenStudy (curry):

how do I know if I'm targetting the ARMv6?

OpenStudy (curry):

I have to leave now, but i'll ocme back and post the results of the compilation.

OpenStudy (curry):

BUt thanks for all your help.

OpenStudy (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] */

OpenStudy (anonymous):

then for get_min it's somewhat simpler since you just want ls->sortedList[0]

OpenStudy (curry):

how would i change it for min?

OpenStudy (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]?

OpenStudy (curry):

@oldrin.bataku

OpenStudy (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.

OpenStudy (curry):

wait isn't sizeof(ls->sortedList) the amount i malloced?

OpenStudy (curry):

or is it just the size of the int pointer?

OpenStudy (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

OpenStudy (anonymous):

@Curry well ls->sortedList is just int * so it's just the size of a pointer (you said 4 bytes)

OpenStudy (curry):

move [r0 + 0] to A /* now A = ls->sortedList[0] */

OpenStudy (curry):

is that right ^^

OpenStudy (anonymous):

er oops, it should move [r0+4] as before and then multiply that by 4 to get a byte offset from ls->sortedList

OpenStudy (anonymous):

well, sortedList[0] is *(sortedList + 0). The actual address is sortedList + 0

OpenStudy (anonymous):

@Curry no, that is A = ls->sortedList or &sortedList[0]

OpenStudy (curry):

hmm, so for max val, the sudo code should be

OpenStudy (anonymous):

ls is a pointer; dereferencing it gives us the value at *ls which is sortedList, a pointer to the array of the values

OpenStudy (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] */

OpenStudy (curry):

so do i deference it twice?

OpenStudy (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

OpenStudy (curry):

and for min, it'd be mov r0 to A, multiply A by 4, add r0 to A and move to r0?

OpenStudy (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]

OpenStudy (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]

OpenStudy (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

OpenStudy (curry):

OMG THANK YOU GUS SO MUCH. <3

OpenStudy (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?

OpenStudy (curry):

@e.mccormick

OpenStudy (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

OpenStudy (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 *);

OpenStudy (anonymous):

then link the C, ASM object files together

OpenStudy (anonymous):

you should not be passing that assembly code to gcc; presumably you need to use a separate assembler (not GCC's as)

OpenStudy (curry):

well i have a header file, that has it, is that the same thing as having external symbols?

OpenStudy (anonymous):

well, by default the C compiler assumes the storage class of a declaration is extern, so that's fine

OpenStudy (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

OpenStudy (curry):

oo! kk. and does my asm code look aprox right?

OpenStudy (curry):

and you said, when i'm compiling the files, i don't need to include the ,s files?

OpenStudy (curry):

so right now, i'm doing gcc -o array array_test.c arraysort.c. Do i need to add any other falgs ?

OpenStudy (anonymous):

what did your professor say to use for writing code for your raspberry pi?

OpenStudy (curry):

what do you mean? like what language?

OpenStudy (anonymous):

no, what toolchain? you're using a build of gcc that does not target ARM

OpenStudy (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?

OpenStudy (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?

OpenStudy (curry):

that's what I'm guessing to. Haven't really tried it. What exactly is a toolchain?

OpenStudy (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

OpenStudy (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

OpenStudy (curry):

@oldrin.bataku

OpenStudy (anonymous):

yeah, you need to assemble the files and link the object code from gcc and your assembler or better yet just gcc -o <C files> <as files> like you had before

OpenStudy (anonymous):

er gcc -o array array_test.c arraysort.c whatever-else.as

OpenStudy (curry):

can i not deference the first operand?

OpenStudy (curry):

and should it be mov r1, [r0+4]? instead of r1 being the 2nd operand?

OpenStudy (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.

OpenStudy (anonymous):

gas uses mov dst, src i believe

OpenStudy (anonymous):

mul doesn't take only one operand; can you show your error?

OpenStudy (anonymous):

as a variable outside a function argument, int[5] is not reduced to a mere pointer

OpenStudy (anonymous):

and it's arrays are never lvalues in C so you can't assign to them like that

OpenStudy (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|

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!