How Do You Write a MIPS Function to Horizontally Flip an Image?

In summary: # $a0 - address of row# $a1 - number of pixels in row# $t0 - num pixels/ 2# $t1 - end address of row# $t6 - pixel from left half# $t7 - pixel from right halfadd $t2, $0, $0 # set the counter variable to 0srl $t0, $a1, 1 # divide the number of pixels by 2 which is the number of times we need # loop to flip itadd
  • #1
SpiffyEh
194
0

Homework Statement



The goal of this problem is to write a MIPS function flipimage which flips an image
horizontally. For example, a simple image is shown on the left, and its flip is shown on
the right.
A picture is composed of individual dots, or pixels, each of which will be represented by
a single byte. The entire two-dimensional image is then stored in memory row by row.
For example, we can store a 4 x 6 picture in memory starting at address 1000 as follows:
• The first row (consisting of 6 pixels) is stored at addresses 1000-1005.
• The second row is stored at addresses 1006-1011.
• The third row is stored at 1012-1017
• The last row is stored at addresses 1018-1023.
(a) Write a MIPS function fliprow to flip a single row of pixels. The function has two
arguments, passed in $a0 and $a1: the address of the row and the number of pixels in that
row. There is no return value. Be sure to follow all MIPS calling conventions


The Attempt at a Solution


So, this is what I have but I'm not sure if this is right or if I'm approaching this all wrong.

# $a0 - address of row
# $a1 - number of pixels in row
# $t2 - counter variable
# $t0 - num pixels/ 2
# $t1 - end address of row
# $t6 - pixel from left half
# $t7 - pixel from right half

add $t2, $0, $0 # set the counter variable to 0
srl $t0, $a1, 1 # divide the number of pixels by 2 which is the number of times we need
# loop to flip it
add $t1, $0, $a1 # put the number of pixels into t1
add $t1, $a0, $t1 # add to the beginning address to get to the end address

loop:
lb $t6, 0($a0) # load the byte into t6 (left half)
lb $t7, 0($t1) # load the byte into $t7 ( right half)
sb $t6, 0($t1) # store t6 at position t1, basically swap
sb $t7, 0($a0) # store t7 at position a0, swapping
addi $a0, $a0, 1 # get the address of the next pixel by adding 8
addi $t1, $t1, -1 # get the address of the previous pixel from end by adding -8
addi $t2,$t2, 1 # increment the counter by 1
bne $t0, $t2, loop # while the counter is not equal to half the number of pixels loop


Can someone please tell me if this seems to be right? And if not where I'm going wrong? An explination would really help too, I'm new to assembly so it's not completely clear to me. Thank you so much
 
Last edited:
Physics news on Phys.org
  • #2
You have the basic idea, but there are several mistakes. For one thing, you should address bytes, not bits, so you want to loop on the number of bytes. Second, you load bytes but then you store words. Also, your calculations seem to be off. When you swap the first row, you do three swaps: 1000<->1005, 1001<->1004, 1002<->1003. Make sure your variables have the right values; it's a common error to be off by 1. Finally, I'm not familiar with MIPS calling conventions, but I don't think you've considered that yet.
 
  • #3
oops, the lw and sw's were my stupid mistake. I don't really need to store anything with the calling conventions because I'm not calling anything else from thsi function. How would I go about getting to each byte that needs to be loaded? I figured if i just added 8 to the address it would get me to the next byte location. Is this incorrect? Also where am I doing a third swap? I don't see where you see it. I fixed the loads/stores above, thank you
 
  • #4
You don't address by bit; you address by byte. Each address corresponds to the location of a byte, so if you increment the address by one, you have the address of the next byte.

The three swaps I mentioned are the ones that are supposed to happen when you flip the first row. My point was you should make sure your routine actually does those swaps, rather than, say, 1000<->1006, etc. When you enter the routine, $a0 will contain 1000, and $a1 will contain 6. Right before you enter the loop for the first time, what values are in $t0, $t1, and $t2? Are they correct?
 
  • #5
Oh I see, i corrected it to increment by 1. I didn't realize it was addressed by bytes.

When you said three swaps I thought you meant that my program was doing 3 each time it looped. I understand what you mean now. I'm pretty sure $t0, $t1, and $t2 are correct. Once its done byte wise I just added the number of bytes which is the number of pixels to the starting address which gives me the ending address in $t1. $t2 starts at 0 which is correct and $t0 is the number of pixels divided by 2 and if I'm correct, if the number is odd it should still return an even number for example 5/2 returns 2 So I don't see anything wrong with those... unless there's something I'm completely missing
 
  • #6
SpiffyEh said:
Oh I see, i corrected it to increment by 1. I didn't realize it was addressed by bytes.

When you said three swaps I thought you meant that my program was doing 3 each time it looped. I understand what you mean now. I'm pretty sure $t0, $t1, and $t2 are correct. Once its done byte wise I just added the number of bytes which is the number of pixels to the starting address which gives me the ending address in $t1.
If $a0 is 1000 and $a1 is 6, what will be in $t1. Is it the address of the last pixel of the first row as you want it to be?
 
  • #7
vela said:
If $a0 is 1000 and $a1 is 6, what will be in $t1. Is it the address of the last pixel of the first row as you want it to be?

ohhh that makes more sense. Sorry, I don't know why, i just didn't see it. So, I guess the last address would be the beginning plud the number of pixels minus 1
 

Related to How Do You Write a MIPS Function to Horizontally Flip an Image?

1. What is assembly coding?

Assembly coding is the process of writing instructions in assembly language, which is a low-level programming language that is closely related to the machine code of a specific computer architecture. It is used to directly control the hardware and perform tasks such as data manipulation, input/output operations, and program control.

2. What is a loop in assembly coding?

A loop in assembly coding is a programming construct used to repeat a set of instructions until a certain condition is met. It is commonly used for tasks such as iterating through an array or performing a repetitive task a specific number of times.

3. How do you code a loop in assembly?

To code a loop in assembly, you first need to define the starting point and ending point of the loop. Then, you use a conditional jump instruction to check if the loop should continue or terminate. Inside the loop, you can perform the desired instructions and update the loop counter until the condition is met.

4. What are the advantages of using loops in assembly coding?

One of the main advantages of using loops in assembly coding is that they allow for efficient use of system resources. By repeating a set of instructions, you can save time and memory space. Additionally, loops can simplify complex tasks and make the code more organized and readable.

5. Are there any drawbacks to using loops in assembly coding?

One potential drawback of using loops in assembly coding is the risk of creating an infinite loop, which can cause the program to crash or consume excessive resources. It is important to carefully design and test loops to avoid this issue. Additionally, loops may not be the most efficient solution for all tasks, so it is important to consider alternative approaches as well.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • 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
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
16K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
14K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
9K
Back
Top