A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 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.

  • This Question is Closed
  1. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  2. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Can you tell me what instructions you have avaiable?

  3. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Assembly languages differ

  4. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    What do you mean by which instructions?

  5. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    sorry, i am very new to assembly. :(

  6. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  7. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes, all those are availabe.

  8. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    THose are actually the ones we learned.

  9. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  10. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Show me what you have so far.

  11. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  12. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Do you have something like 'jz'?

  13. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  14. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  15. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Then convert it and optimize it.

  16. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  17. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  18. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes.

  19. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  20. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    How is `list` defined?

  21. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  22. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    um, list is in a struct. one sec.

  23. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  24. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    the C code containing the struct and stuff

  25. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    32 bit.

  26. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  27. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  28. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  29. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  30. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    BUt thanks for all your help.

  31. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  32. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  33. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    how would i change it for min?

  34. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    why would moving r0 + 4 to a mean ls-> size? wouldn't that just be the first 4 bytes? which is ls->sortedList[0]?

  35. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @oldrin.bataku

  36. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  37. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  38. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    or is it just the size of the int pointer?

  39. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  40. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  41. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  42. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    is that right ^^

  43. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  44. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  45. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  46. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  47. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  48. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  49. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    so do i deference it twice?

  50. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  51. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  52. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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]

  53. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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]

  54. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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

  55. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    OMG THANK YOU GUS SO MUCH. <3

  56. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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?

  57. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @e.mccormick

  58. e.mccormick
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  59. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    you need to tell the C compiler than you have external symbols: extern int get_max_ARM(list *); extern int get_min_ARM(list *);

  60. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    then link the C, ASM object files together

  61. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  62. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  63. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  64. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  65. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  66. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  67. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  68. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  69. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    what do you mean? like what language?

  70. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  71. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  72. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  73. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  74. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  75. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  76. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @oldrin.bataku

  77. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  78. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  79. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    can i not deference the first operand?

  80. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  81. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  82. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    gas uses mov dst, src i believe

  83. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  84. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  85. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  86. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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|

  87. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @oldrin.bataku

  88. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  89. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.