Assembly language problem [repost using template :D]

In summary: 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?In summary, Jedishrfu is trying to add the address of A array with a variable, but he should multiply it by 4 to get the offset.
  • #1
Khaled Kord
8
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
  • #2
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: 414
  • #3
What is the array type?
 
  • #4
cpscdave said:
What is the array type?
32-bit integers
 
  • #6
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 jedishrfu
  • #7
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 ! :))
 
  • #8
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 :)
 
  • #9
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: 370
  • #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:

Related to Assembly language problem [repost using template :D]

1. What is assembly language and how is it different from high-level programming languages?

Assembly language is a low-level programming language that is used to directly communicate with a computer's hardware. Unlike high-level programming languages, it uses mnemonics to represent individual machine instructions, making it more difficult for humans to read and write. However, it allows for precise control over the computer's operations and can be highly efficient.

2. What are some common challenges when working with assembly language?

One of the main challenges of working with assembly language is its complexity and steep learning curve. It requires a deep understanding of computer architecture and hardware, and even a small mistake can result in a program that does not work properly. Debugging can also be difficult, as there are no built-in error messages or debugging tools.

3. Can assembly language be used for all types of applications?

While assembly language can technically be used for any type of application, it is most commonly used for low-level programming tasks such as operating systems, device drivers, and firmware. Its complexity and lack of portability make it less practical for larger and more complex programs.

4. Is it necessary to learn assembly language as a scientist?

This depends on the specific field of science and the type of work being done. For scientists working with embedded systems or developing specialized software, knowledge of assembly language may be beneficial. However, for many scientific tasks, high-level programming languages are more than sufficient.

5. How can I learn assembly language?

There are various resources available for learning assembly language, including online tutorials, books, and classes. It is recommended to have a strong understanding of computer architecture and a background in programming before attempting to learn assembly language. Practicing and working on small projects can also help improve proficiency in the language.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
10K
Back
Top