Assembly language problem [repost using template :D]

Click For Summary

Discussion Overview

The discussion revolves around converting a specific C statement into assembly language, focusing on the correct handling of array indexing and pointer arithmetic. Participants are analyzing a manual solution and comparing it with their own attempts, particularly regarding the treatment of offsets in assembly language.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Participants are tasked with converting the C statement B[8] = A[i-j] into assembly language, given specific register assignments for variables and base addresses for arrays.
  • One participant questions the manual solution's approach, specifically the addition of the address of array A with variable f instead of multiplying f by 4 to obtain the correct offset.
  • Another participant clarifies that in C, pointer arithmetic automatically accounts for the size of the data type, which is why adding one to an int pointer results in an address 4 bytes higher.
  • There is a discussion about the necessity of manual multiplication in assembly language for calculating offsets, contrasting it with C's automatic handling of pointer arithmetic.
  • Participants express confusion about the manual solution's omission of the multiplication step, leading to further inquiries about the values of the registers involved and the operations performed in the assembly code.
  • Specific assembly instructions are mentioned, such as sub, add, lw, and sw, with participants seeking clarification on their functions and the overall logic of the code.

Areas of Agreement / Disagreement

Participants do not reach a consensus regarding the correctness of the manual solution. There are multiple competing views on how the assembly code should be structured and whether the manual solution is accurate in its approach to pointer arithmetic.

Contextual Notes

There is uncertainty regarding the values assigned to the registers and how they affect the assembly code's execution. The discussion also highlights the need for clarity on the specific assembly language being used, as different architectures may have different conventions.

Who May Find This Useful

This discussion may be useful for students learning assembly language, particularly those interested in the intricacies of pointer arithmetic and array handling in both C and assembly contexts.

Khaled Kord
Messages
8
Reaction score
0

Homework Statement



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];

Homework Equations



none

The Attempt at a Solution


image attachedi 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
 
Physics news on Phys.org
Khaled Kord said:

Homework Statement



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];

Homework Equations



none

The Attempt at a Solution


image attachedi 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

the manual solution answer
 

Attachments

  • answ.JPG
    answ.JPG
    4.2 KB · Views: 488
What is the array type?
 
cpscdave said:
What is the array type?
32-bit integers
 
Khaled Kord said:
32-bit integers

Between that and what jedishrfu said you should be able to figure out why its multiplied by 4 :)
 
  • Like
Likes   Reactions: jedishrfu
jedishrfu said:
In C code when you add one to pointer of a given type it will handle the size of the element.

http://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm
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 ! :))
 
cpscdave said:
Between that and what jedishrfu said you should be able to figure out why its multiplied by 4 :)
i know why do we multiply by 4, i was asking why the manual solution didn't :)
 
Khaled Kord said:
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 ! :))
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.
 
  • #10
Mark44 said:
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.

if in assembly i should do this extra, so why the manual solution didn't do that?! "it's answer attached"
 

Attachments

  • answ.JPG
    answ.JPG
    4.2 KB · Views: 457
  • #11
Khaled Kord said:
if in assembly i should do this extra, so why the manual solution didn't do that?! "it's answer attached"
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:
sub $t0, $s3, $s4
add $t0, $s6, $t0
lw $t1, 16($t0)
sw $t1, 32($s7)
Khaled Kord said:
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];
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
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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
11K
  • · Replies 16 ·
Replies
16
Views
11K
  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 4 ·
Replies
4
Views
21K
  • · Replies 6 ·
Replies
6
Views
6K