1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Assembly language problem [repost using template :D]

  1. Feb 29, 2016 #1
    1. The problem statement, all variables and given/known data

    given that f,g,h,i and j are assigned to registers s0,s1,s2,s3 and s4
    and the base addresses for the arrays A,B are in s6,s7

    convert this C statement to assemply language: B[8] = A[i-j];

    2. Relevant equations


    3. The attempt at a solution
    image attached

    i attached both manual solutions and my answers, i can't understand the second line in his, he is adding the address of array A with variable f, while he should multiply f by 4 to get the offset
  2. jcsd
  3. Feb 29, 2016 #2
    the manual solution answer

    Attached Files:

  4. Feb 29, 2016 #3
    What is the array type?
  5. Feb 29, 2016 #4
    32-bit integers
  6. Feb 29, 2016 #5


    Staff: Mentor

  7. Feb 29, 2016 #6
    Between that and what jedishrfu said you should be able to figure out why its multiplied by 4 :)
  8. Feb 29, 2016 #7
    you mean mean if i added a number to a reference it automatically multiply it by the size
    i know about pointers arithmetic from previous programming course, didn't think about it .. thanks ! :))
  9. Feb 29, 2016 #8
    i know why do we multiply by 4, i was asking why the manual solution didn't :)
  10. Mar 1, 2016 #9


    Staff: Mentor

    In C and C++, if you add a number to a pointer, the result of the addition depends on the type of the pointer. So, adding 1 to an int pointer actually results in an address 4 bytes higher in memory than the address in the pointer. Adding 1 to a double pointer results in an address 8 bytes higher in memory. Adding 1 to a char pointer results in an address 1 byte higher in memory.

    Subtracting a number from a pointer works in a similar fashion, but results in addresses that are lower in memory.

    In contrast, when you write assembly code you have to do those extra multiplications yourself.
  11. Mar 2, 2016 #10
    if in assembly i should do this extra, so why the manual solution didn't do that?! "it's answer attached"

    Attached Files:

  12. Mar 2, 2016 #11


    Staff: Mentor

    Posting an image of the code in your book is not very helpful. You should also state what sort of assembly code you're working with. Yours appears to be MIPS.

    Code (Text):
    sub $t0, $s3, $s4
    add $t0, $s6, $t0
    lw $t1, 16($t0)
    sw $t1, 32($s7)
    What are the values of f, g, h, and i? To understand what a piece of code is doing, you have to know what values are being added, subtracted, loaded, or stored.

    In the first line of code, the value in s4 is being subtracted from the value in s3, with the result being placed in t0.
    Start at the first line, and see if you can figure out what gets stored in t0. Then do the same for the second, third, and fourth lines.

    Do you know what the LW and SW instructions do?
    Last edited: Mar 2, 2016
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted