A community for students.
Here's the question you clicked on:
 0 viewing
Curry
 one year ago
How do I write a get_min code in assembly? The code has to get the minimum value in an array.
Curry
 one year ago
How do I write a get_min code in assembly? The code has to get the minimum value in an array.

This Question is Closed

Curry
 one year ago
Best ResponseYou've already chosen the best response.0I'm doing this in raspberry pi's ARM.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0Can you tell me what instructions you have avaiable?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0Assembly languages differ

Curry
 one year ago
Best ResponseYou've already chosen the best response.0What do you mean by which instructions?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0sorry, i am very new to assembly. :(

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0what operations can you do? Do you have like 'mov' or 'add' or stuff like that?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0yes, all those are availabe.

Curry
 one year ago
Best ResponseYou've already chosen the best response.0THose are actually the ones we learned.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0Do you have sub? What sort of jumps do you have?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0Show me what you have so far.

Curry
 one year ago
Best ResponseYou've already chosen the best response.0I literally have nothing so far. I have no clue how to start.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0Do you have something like 'jz'?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0I 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
 one year ago
Best ResponseYou've already chosen the best response.0Let me think. First let us start with the C code.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0Then convert it and optimize it.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0You return the first and last, not the max and min.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0But since it is sorted, the min is the first?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0it'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
 one year ago
Best ResponseYou've already chosen the best response.0How is `list` defined?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0@wio: in the C code it's given as: typedef struct List { int *sortedList; int size; int maxSize; } list;

Curry
 one year ago
Best ResponseYou've already chosen the best response.0um, list is in a struct. one sec.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0how big are addresses? 64bits? 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
 one year ago
Best ResponseYou've already chosen the best response.0the C code containing the struct and stuff

Curry
 one year ago
Best ResponseYou've already chosen the best response.0i compiled with gcc o S array_test.c arraysort.c

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0remove o and make sure you're compiling this code targeting the ARMv6 for raspberry pi

Curry
 one year ago
Best ResponseYou've already chosen the best response.0how do I know if I'm targetting the ARMv6?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0I have to leave now, but i'll ocme back and post the results of the compilation.

Curry
 one year ago
Best ResponseYou've already chosen the best response.0BUt thanks for all your help.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0well, 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
 one year ago
Best ResponseYou've already chosen the best response.0then for get_min it's somewhat simpler since you just want ls>sortedList[0]

Curry
 one year ago
Best ResponseYou've already chosen the best response.0how would i change it for min?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0why would moving r0 + 4 to a mean ls> size? wouldn't that just be the first 4 bytes? which is ls>sortedList[0]?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0No, 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
 one year ago
Best ResponseYou've already chosen the best response.0wait isn't sizeof(ls>sortedList) the amount i malloced?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0or is it just the size of the int pointer?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0actually 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
 one year ago
Best ResponseYou've already chosen the best response.0@Curry well ls>sortedList is just int * so it's just the size of a pointer (you said 4 bytes)

Curry
 one year ago
Best ResponseYou've already chosen the best response.0move [r0 + 0] to A /* now A = ls>sortedList[0] */

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0er oops, it should move [r0+4] as before and then multiply that by 4 to get a byte offset from ls>sortedList

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0well, sortedList[0] is *(sortedList + 0). The actual address is sortedList + 0

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0@Curry no, that is A = ls>sortedList or &sortedList[0]

Curry
 one year ago
Best ResponseYou've already chosen the best response.0hmm, so for max val, the sudo code should be

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0ls is a pointer; dereferencing it gives us the value at *ls which is sortedList, a pointer to the array of the values

Curry
 one year ago
Best ResponseYou've already chosen the best response.0mov 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
 one year ago
Best ResponseYou've already chosen the best response.0so do i deference it twice?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0well, 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
 one year ago
Best ResponseYou've already chosen the best response.0and for min, it'd be mov r0 to A, multiply A by 4, add r0 to A and move to r0?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0r0+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>size1]

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0for 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
 one year ago
Best ResponseYou've already chosen the best response.0honestly 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
 one year ago
Best ResponseYou've already chosen the best response.0OMG THANK YOU GUS SO MUCH. <3

Curry
 one year ago
Best ResponseYou've already chosen the best response.0@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
 one year ago
Best ResponseYou've already chosen the best response.0I do not use asm or c. But here are some references that look promissing. https://courses.engr.illinois.edu/ece390/books/labmanual/cprogmixing.html http://www.nongnu.org/avrlibc/usermanual/group__asmdemo.html

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0you need to tell the C compiler than you have external symbols: extern int get_max_ARM(list *); extern int get_min_ARM(list *);

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0then link the C, ASM object files together

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0you should not be passing that assembly code to gcc; presumably you need to use a separate assembler (not GCC's as)

Curry
 one year ago
Best ResponseYou've already chosen the best response.0well i have a header file, that has it, is that the same thing as having external symbols?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0well, by default the C compiler assumes the storage class of a declaration is extern, so that's fine

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0those errors are because you're using a nonARM GCC toolchain to build the code; use the setup for compilation you were instructed to use for ARM

Curry
 one year ago
Best ResponseYou've already chosen the best response.0oo! kk. and does my asm code look aprox right?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0and you said, when i'm compiling the files, i don't need to include the ,s files?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0so right now, i'm doing gcc o array array_test.c arraysort.c. Do i need to add any other falgs ?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0what did your professor say to use for writing code for your raspberry pi?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0what do you mean? like what language?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0no, what toolchain? you're using a build of gcc that does not target ARM

Curry
 one year ago
Best ResponseYou've already chosen the best response.0oh, 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
 one year ago
Best ResponseYou've already chosen the best response.0yeah, 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
 one year ago
Best ResponseYou've already chosen the best response.0that's what I'm guessing to. Haven't really tried it. What exactly is a toolchain?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0as 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
 one year ago
Best ResponseYou've already chosen the best response.0I 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

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0yeah, 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

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0er gcc o array array_test.c arraysort.c whateverelse.as

Curry
 one year ago
Best ResponseYou've already chosen the best response.0can i not deference the first operand?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0and should it be mov r1, [r0+4]? instead of r1 being the 2nd operand?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0mul 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
 one year ago
Best ResponseYou've already chosen the best response.0gas uses mov dst, src i believe

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0mul doesn't take only one operand; can you show your error?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0as a variable outside a function argument, int[5] is not reduced to a mere pointer

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0and it's arrays are never lvalues in C so you can't assign to them like that

Curry
 one year ago
Best ResponseYou've already chosen the best response.0so 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

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0yes, 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
 one year ago
Best ResponseYou've already chosen the best response.0that 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
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.