• Support PF! Buy your school textbooks, materials and every day products Here!

Assembly, basically coding a loop

  • Thread starter SpiffyEh
  • Start date
  • #1
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:

Answers and Replies

  • #2
vela
Staff Emeritus
Science Advisor
Homework Helper
Education Advisor
14,619
1,253
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
194
0
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
vela
Staff Emeritus
Science Advisor
Homework Helper
Education Advisor
14,619
1,253
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
194
0
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 theres something i'm completely missing
 
  • #6
vela
Staff Emeritus
Science Advisor
Homework Helper
Education Advisor
14,619
1,253
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
194
0
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 Threads on Assembly, basically coding a loop

  • Last Post
Replies
3
Views
9K
Replies
2
Views
13K
  • Last Post
Replies
5
Views
14K
Replies
16
Views
7K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
1
Views
931
Replies
2
Views
657
Replies
10
Views
12K
Replies
4
Views
624
Replies
14
Views
4K
Top