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

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.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions.

A community for students.

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

Computer Science
See more answers at brainly.com
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.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

I'm doing this in raspberry pi's ARM.
Can you tell me what instructions you have avaiable?
Assembly languages differ

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

What do you mean by which instructions?
sorry, i am very new to assembly. :(
what operations can you do? Do you have like 'mov' or 'add' or stuff like that?
yes, all those are availabe.
THose are actually the ones we learned.
Do you have sub? What sort of jumps do you have?
Show me what you have so far.
I literally have nothing so far. I have no clue how to start.
Do you have something like 'jz'?
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.
Let me think. First let us start with the C code.
Then convert it and optimize it.
You return the first and last, not the max and min.
But since it is sorted, the min is the first?
yes.
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
How is `list` defined?
@wio: in the C code it's given as: typedef struct List { int *sortedList; int size; int maxSize; } list;
um, list is in a struct. one sec.
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
the C code containing the struct and stuff
32 bit.
i compiled with gcc -o -S array_test.c arraysort.c
remove -o and make sure you're compiling this code targeting the ARMv6 for raspberry pi
how do I know if I'm targetting the ARMv6?
I have to leave now, but i'll ocme back and post the results of the compilation.
BUt thanks for all your help.
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] */
then for get_min it's somewhat simpler since you just want ls->sortedList[0]
how would i change it for min?
why would moving r0 + 4 to a mean ls-> size? wouldn't that just be the first 4 bytes? which is ls->sortedList[0]?
@oldrin.bataku
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.
wait isn't sizeof(ls->sortedList) the amount i malloced?
or is it just the size of the int pointer?
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
@Curry well ls->sortedList is just int * so it's just the size of a pointer (you said 4 bytes)
move [r0 + 0] to A /* now A = ls->sortedList[0] */
is that right ^^
er oops, it should move [r0+4] as before and then multiply that by 4 to get a byte offset from ls->sortedList
well, sortedList[0] is *(sortedList + 0). The actual address is sortedList + 0
@Curry no, that is A = ls->sortedList or &sortedList[0]
hmm, so for max val, the sudo code should be
ls is a pointer; dereferencing it gives us the value at *ls which is sortedList, a pointer to the array of the values
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] */
so do i deference it twice?
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
and for min, it'd be mov r0 to A, multiply A by 4, add r0 to A and move to r0?
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]
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]
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
OMG THANK YOU GUS SO MUCH. <3
@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?
@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
you need to tell the C compiler than you have external symbols: extern int get_max_ARM(list *); extern int get_min_ARM(list *);
then link the C, ASM object files together
you should not be passing that assembly code to gcc; presumably you need to use a separate assembler (not GCC's as)
well i have a header file, that has it, is that the same thing as having external symbols?
well, by default the C compiler assumes the storage class of a declaration is extern, so that's fine
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
oo! kk. and does my asm code look aprox right?
and you said, when i'm compiling the files, i don't need to include the ,s files?
so right now, i'm doing gcc -o array array_test.c arraysort.c. Do i need to add any other falgs ?
what did your professor say to use for writing code for your raspberry pi?
what do you mean? like what language?
no, what toolchain? you're using a build of gcc that does not target ARM
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?
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?
that's what I'm guessing to. Haven't really tried it. What exactly is a toolchain?
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
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
@oldrin.bataku
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
er gcc -o array array_test.c arraysort.c whatever-else.as
can i not deference the first operand?
and should it be mov r1, [r0+4]? instead of r1 being the 2nd operand?
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.
gas uses mov dst, src i believe
mul doesn't take only one operand; can you show your error?
as a variable outside a function argument, int[5] is not reduced to a mere pointer
and it's arrays are never lvalues in C so you can't assign to them like that
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|
@oldrin.bataku
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
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

Not the answer you are looking for?

Search for more explanations.

Ask your own question