- #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: