So I am writing an assembly program that has lots of constant integer values.
I know that in the .data section I can assign a label with a .word data type and type in my number. With this method I still have to load an address in main.
But in main, I could just simply use
li $t1, some_number
Are any one of these methods better than the other and why?
Generally I'd say using li is the better approach. You're avoiding adding a bunch of clutter in your .data section, and you will also get more efficient code in some cases.
Let's look at some examples:
.data
ten: .word 10
million: .word 1000000
.text
main:
lw $t0,ten
li $t1,10
lw $t2,million
li $t3,1000000
It's important to understand here that both lw and li are pseudo-instructions that get translated into one or more actual instructions. lw does exist in the MIPS instruction set, but this particular variant of it doesn't. li doesn't exist in the MIPS instruction set.
If we look at what SPIM generates for the first two instructions, we see:
[0x00400024] 0x3c011001 lui $1, 4097 ; 9: lw $t0,ten
[0x00400028] 0x8c280000 lw $8, 0($1)
[0x0040002c] 0x3409000a ori $9, $0, 10 ; 10: li $t1,10
So that's one additional instruction for the lw variant, as the address first has to be loaded into a register, and then the value is loaded from that address. This also means one additional (potentially slow) memory access (well, two if you count the instruction fetch).
Now let's look at the other two instructions, where the value to be loaded is too large to be encoded in a single instruction:
[0x00400030] 0x3c011001 lui $1, 4097 ; 11: lw $t2,million
[0x00400034] 0x8c2a0004 lw $10, 4($1)
[0x00400038] 0x3c01000f lui $1, 15 ; 12: li $t3,1000000
[0x0040003c] 0x342b4240 ori $11, $1, 16960
Here the immediate 1000000 is loaded using two instructions as (15 << 16) | 16960. So both variants require two instructions, but the li variant doesn't need to read from memory.
If you want to assign a meaningful name to a constant to avoid having magic numbers all over your code you can do so with =:
TEN = 10
li $t0, TEN # Expands to li $t0, 10
You could perhaps avoid loading the addresses for lw all the time by using $gp-relative addressing, but I feel that that's beyond the scope of this question.
Related
I am having a hard time figuring out where to start with this project. I am needing to write code in PLP that is a palindrome checker.
the task is to write a program that recieves a string of characters via UART, checks if this string is a palindrome, then uses a print function to print either"yes" of "no". I have been given a template that I am to follow when creating the program.
The template project file contains six function stubs that need to be implemented. five are called from the main loop and the sixth is called from "period_check: in the template file it contains descriptions of what each function needs to do and how it should be implemented. I have attempted to fill in some, however I do not think I am on the right track. Please help.
***** I have gotten this much code in, but it does not print out the right output****
it prints no for everything vs no for non palindromes and yes for palindrome.
.org 0x10000000
# Initializations
# NOTE: You may add initializations after line 10, but please do not
# remove or change the initializations to $sp, $s0, $s1, or $s2
li $sp, 0x10fffffc # Starting address of empty stack
li $s0, 0xf0000000 # UART base address
li $s1, array_ptr # Array head pointer
li $s2, array_ptr # Array tail pointer
####################################################################
# Do not make changes to the jump to main, the allocation of
# memory for the array, or the main loop
####################################################################
j main
nop
array_ptr: # Label pointing to 100 word array
.space 100
main:
jal poll_UART
nop
jal period_check
nop
jal space_check
nop
jal case_check
nop
jal array_push
nop
j main
nop
####################################################################
# ******************************************************************
####################################################################
# The "poll_UART" function should poll the status register of the UART.
# If the 2^1 bit position (ready bit) is set to 1 then it
# should copy the receive buffer's value into $v0 and send
# a clear status command (2^1) to the command register before
# returning (a return statement is already included). In order to
# receive full credit, $s0 must contain the base address of the UART
# and must be used with the appropriate offsets to access UART
# registers and buffers
poll_UART:
lw $t1, 4($s0)
li $t2, 0b10
and $t3, $t1, $t2
beq $t3, $0, main
nop
lw $v0, 8($s0)
sw $t2, 0($s0)
jr $ra
nop
# The "period_check" function should check if the current character ($v0)
# is a period ("."). If it is a period then the function should go to the
# label, "palindrome_check". If the character is not a period then it
# should use the included return.
period_check:
li $t0, 0x2E
beq $v0, $t0, palindrome_check
nop
# The "space_check" function should check if the current character ($v0)
# is a space (" "). If it is then it should jump to "main" so
# that it skips saving the space character. If not it should
# use the included return.
space_check:
li $t4, 0x20
beq $t4, $v0, main
jr $ra
nop
# The "case_check" function should perform a single inequality check.
# If the current character ($v0) is greater than the ASCII value of 'Z',
# which indicates the current character is lowercase, then it should convert
# the value of $v0 to the uppercase equivalent and then return. If the
# current character ($v0) is already uppercase (meaning the inequality
# mentioned before was not true) then the function should return without
# performing a conversion.
case_check:
li $t5, 0x5A
slt $t6, $v0, $t5
li $t7, 1
beq $t6, $t7, convert
convert:
addiu $v0, $v0, -32
jr $ra
nop
# The "array_push" function should save the current character ($v0) to the
# current location of the tail pointer, $s2. Then it should increment the
# tail pointer so that it points to the next element of the array. Last
# it should use the included return statement.
array_push:
sw $v0, 0($s2)
addiu, $s2, $s2, 4
jr $ra
nop
# The "palindrome_check" subroutine should be jumped to by the period
# check function if a period is encountered. This subroutine should contain
# a loop that traverses the array from the front towards the back (using the
# head pointer, $s1) and from the back towards the front(using the tail
# pointer, $s2). If the string is a palindrome then as the array is traversed
# the characters pointed to should be equal. If the characters are not equal
# then the string is not a palindrome and the print function should be used
# to print "No". If the pointers cross (i.e. the head pointer's address is
# greater than or equal to the tail pointer's address) and the compared
# characters are equal then the string is a palindrome and "Yes" should be
# printed.
#
# Remember to restore the head and tail pointers to the first element
# of the array before the subroutine jumps back to main to begin processing the
# next string. Also, keep in mind that because the tail pointer is updated at
# the end of "array_push" it technically points one element past the last
# character in the array. You will need to compensate for this by either
# decrementing the pointer once at the start of the array or using an offset
# from this pointer's address.
palindrome_check:
addiu $s2, $s2, -8
move $s3, $s1
subu $s6, $s2, $s3
beq $s6, $0, palindrome
nop
check_loop:
lw $s4, 0($s3)
lw $s5, 0($s2)
bne $s5, $t0, not_palindrome
nop
adjust_pointers:
addiu $s2, $s2, -4
addiu $s3, $s3, 4
slt $t8, $s3, $s2
bne $t8, $t0, check_loop
nop
j palindrome
nop
palindrome:
li $a0, 1
call project3_print
move $s2, $s1
j main
not_palindrome:
li $a0, 0
call project3_print
move $s2, $s1
j main
nop
Ok, this is just my opinion, but you are definitely not on the right track.
The control flow you're showing is problematic.
To see one reason why, try writing this same in C or any other language that you know. You won't be able to do it because of the non-local goto's that's using, where one procedure jumps (without calling) to another procedure.
Further, finding whether an input is a palindrome is not a fixed sequence of one-time steps that are executed on each input character.
You will (1) need to store the characters for later comparison, and (2) need a decision point where you can determine (and print) yes it is, or no it isn't. You don't have any control structure for that.
that recieves a string of characters via UART, checks if this string is a palindrome, then uses a print function to print either"yes" of "no".
Yes, your main should reflect the above description you've been given:
receive a string of characters
checks if this string is a palindrome
print either "yes" of "no"
In other words you might have something like:
int len = input_string();
if ( check_palindrome(len) ) {
print "yes";
else
print "no"
Suggest you write it in C or other language you know, then translate that to assembly.
Also consider that we some things we program are functions returning a value rather than procedures that don't return values. Returning a value so that main can take a different course of action (e.g. print yes vs. no) is much better than using non-local goto's to alter the flow of control from within a subroutine.
If your instruction/coursework has given you that main, and is recommending non-local goto's that would be very sad.
I feel for you and your classmates, as this is one of the worst examples of teaching assembly I've seen in a long long time.
array_ptr: # Label pointing to 100 word array
.space 100
The label name is misleading. This space is used as an array of words, not a pointer to an array. The storage reserved is 25 words, since .space operates in terms of bytes and words are 4 bytes each. So, the comment is just plain wrong.
The various "functions" called using jal are single use function, so there's really no need for functions in this assignment at all. The "functions" also are going to each other and back to main instead of returning properly like they would in structured programming. So, this is what we call spaghetti code — such code is difficult to reason over and one of the reasons that other languages don't even bother to offer this kind of flow control.
The array being used is storing whole words, when the input elements are only characters, so that's harmless but unnecessary.
beq $t6, $t7, convert
convert:
This control structure will never choose between two options, it will always convert. Why? Because in the case $t6 is true it will branch to convert: and in the case that $t6 is not true it will fall through to convert:, so same location, will run same code in either case.
You should be able to observe this during debugging.
Debugging Tips
Get to know your data. You should know the address of the array as you debug. You can find this during execution, e.g. look at a register after li ... array_ptr (btw, that opcode should be la, but no matter if it works). Otherwise you can observe the data section and its layout to find that out before running the first instruction.
Single step each line like one would to debug code in any other language, verifying program state between each line. In MIPS assembly, not much program state changes between lines so usually this is pretty simple — usually each instruction only changes one register or one memory location — but you must verify that such change is as you're expecting. Once the first part of the program is properly storing characters into the array, you can use the break point feature to stop at the palindrome check routine and single step only from there on.
Use the smallest possible input first, (in the most degenerate case that would be an empty string, but you may not be handling those so instead) might try a single letter input (should be a palindrome). Once that is working, try two letter input. As I said, first make sure that the character values are being placed into the array properly, and only when you've verified that's working, go on to debug the palindrome check code.
Supposing that f, g, h, i are stored in $s0~$s4 respectively and the base addresses of arrays A and B are in $S6 and $S7.
sll $t0, $s0, 2
add $t0, $s6, $t0
sll $tl, $sl, 2
add $tl, $s7, $tl
lw $s0, 0($t0)
addi $t2 , $t0, 4
lw $t0, 0($t2)
add $t0, $t0, $s0
SW $t0, 0($tl)
I'm not familiar with MIPS so I Wonder how to translate MIPS into C and how to minimize these MIPS instructions?
how to translate MIPS into C
You recognize the patterns, here for array indexing / array element access.
On a byte addressable machine (all modern hardware), a 4-byte integer occupies 4 bytes in memory, and each of those bytes has a unique memory address. Because of the way the hardware works, we only use one of those 4 addresses to refer to the whole 4-byte integer, namely we use the lowest address among the 4. The hardware can load a 4-byte integer from memory given that one address (the lowest).
Since each 4-byte integer in memory occupies 4 addresses, in an array of 4-byte integers, the memory address of the first element and the memory address of the second element are 4 addresses apart even though are sequential index positions (i.e. they are only 1 index position apart).
The formula for indexing a 4-byte integer array, then is to convert the index into a byte offset, then add the byte offset to the base address of the array. The first part of that: converting an index to a byte offset, is sometimes referred to as "scaling". Scaling is conceptually done by multiplication, so in A[i], i needs to be scaled by the size of the array elements of A. If 4-byte integers that means scaling (multiplying) the index by 4. A quick way of doing that is shifting by 2 bit positions, which has the same effect as multiplying by 4.
The C language automatically scales when doing array references, whereas assembly language requires explicit scaling. C can do this because it knows the type of the array, whereas assembly language does not.
In C we can do expressions like A[i]. The C language allows us to break that down somewhat into *(A+i), which separates the pointer arithmetic addition A+i from the dereferencing of that sum, dereferencing with the unary indirection operator, *. As previously mentioned, C automatically scales, so A+i becomes the equivalent of A+i*4, in which we can substitute shifting for multiplication: A+(i<<2).
Next, we need to know if the dereference is for read or for write. When A[i] is accessed for its value, we will see it on what we call the "right hand side" of an assignment operator, as in ... = A[i]. When A[i] is access to update/store a value, we will see it on what we call the left hand side of an assignment operator, as in A[i] = ....
So, the sequence for doing A[i] for read (right hand side) in C is the following in assembly:
sll $temp1, $i, 2
addu $temp2, $A, $temp1
lw $temp3, 0($temp1)
Where $tempN is some register (usually a designated temporary) chosen to hold an intermediate value. Since multiple instructions are needed to accomplish anything, sequences of instructions are interconnected with registers that hold the intermediate states. And also, in assembly we name registers, not variables, so in my above $i and $A should be a registers names representing those variables rather than variable names directly used.
The pattern for write/store array access is similar but ends with a sw instruction instead, to store some value into memory at the index position.
These instruction sequence are interconnected by the use of these registers, and the sequences can be interrupted or interspersed with other instructions — what we have to follow then is the above pattern by paying attention to to the register usages that interconnect them rather than the specific sequences.
In your sample code:
sll $t0, $s0, 2 # sourcing an index in $s0, scaling it into temp $t0
add $t0, $s6, $t0 # adding a base array in $s6, putting back into $t0
sll $tl, $sl, 2
add $tl, $s7, $tl
lw $s0, 0($t0) # accessing the value of $s6[$s0*4], aka A[f]
addi $t2 , $t0, 4
lw $t0, 0($t2)
add $t0, $t0, $s0
SW $t0, 0($tl)
We can see the pattern for a read access to an index in $s0, and an array in $s6, these, we are told, map to f and A, so those three instructions comprise A[f] to read a value from A at index f.
The rest are done similarly. Your job is to use this knowledge to find the other array indexing patterns in the above sequence. Find out how the results of the array indexing operations are used and you'll have the complete C code.
NOTE that the sample you've been given incorrectly uses add and addi when pointer arithmetic should use addu and addiu — we don't want signed integer overflow checking on pointer arithmetic, as pointers are unsigned.
One of the add instructions is not for pointer arithmetic, but should probably still have used addu if this is intended to be replicated in C, because the C language does not have a built in operator to trap on overflow.
I see people using lw and li for the same purpose, of storing and manipulating numbers in registers. But which one is the most adequate, under what circumstances?
Thank you.
I see people using lw and li for the same purpose
They don't have the same purpose.
The purpose of lw is to load a word (32 bits) from memory into a register. For example:
.data
foo: .word 12345
.text
la $a0, foo # $a0 = address of foo
lw $t0, ($a0) # $t0 = 12345
The purpose of li is to load an immediate into a register. For example:
li $t0, 12345 # $t0 = 12345
Note that the MIPS instruction set doesn't actually include any li instruction. It's a pseudo-instruction implemented by the assembler, which will convert it into one or more actual instructions (e.g. a combination of lui and ori).
How do I calculate the sum of odd positive integers in MIPS? I have a MIPS simulator at home and I use to book to help verify my work. My university has a computer lab that has hardware provided by an outside company. The idea I suppose is that the University "pimps out" the hardware to students through the classes. Part of the problem for me is that I want to verify my code work properly, while using the board at school but it seems easier to verify the code works at home. Anyway, I think the code should read something like this:
andi $t8, $s0, 1 #value from $s0 and add 1 to it. Place in $t8 register
bnez $t8 #This should determine if its odd
beqz $79 #This should determine if its even
even:
addi $t7, $t8, -1
bnez $t7, odd
odd:
addi $t6, $t7, -2
Rt6, loop
Is there an easier way to do this? I need to write a main routine in which at the end of the execution perform v0=the sum of odd positive integers between 1 and 1000. $t8 is my $v0 in this case. Any helpful suggestions would be considered very closely.
Here's some annotated code that does the sum of both odd and even values. It also has an example of a subroutine.
.data
array:
.word 17767, 9158, 39017, 18547
.word 56401, 23807, 37962, 22764
.word 7977, 31949, 22714, 55211
.word 16882, 7931, 43491, 57670
.word 124, 25282, 2132, 10232
.word 8987, 59880, 52711, 17293
.word 3958, 9562, 63790, 29283
.word 49715, 55199, 50377, 1946
.word 64358, 23858, 20493, 55223
.word 47665, 58456, 12451, 55642
arrend:
msg_odd: .asciiz "The sum of the odd numbers is: "
msg_even: .asciiz "The sum of the even numbers is: "
msg_nl: .asciiz "\n"
.text
.globl main
# main -- main program
#
# registers:
# t0 -- even sum
# t1 -- odd sum
# t2 -- current array value
# t3 -- isolation for even/odd bit
# t6 -- array pointer
# t7 -- array end pointer
main:
li $t0,0 # zero out even sum
li $t1,0 # zero out odd sum
la $t6,array # address of array start
la $t7,arrend # address of array end
main_loop:
bge $t6,$t7,main_done # are we done? if yes, fly
lw $t2,0($t6) # get value
addiu $t6,$t6,4 # point to next array element
andi $t3,$t2,1 # isolate LSB
beqz $t3,main_even # is is even? if yes, fly
add $t1,$t1,$t2 # add to odd sum
j main_loop
main_even:
add $t0,$t0,$t2 # add to even sum
j main_loop
main_done:
# output the even sum
la $a0,msg_even
move $a1,$t0
jal print
# output the odd sum
la $a0,msg_odd
move $a1,$t1
jal print
# terminate program
li $v0,10
syscall
# print -- output a number
#
# arguments:
# a0 -- pointer to message
# a1 -- number to output
print:
# output the message
la $v0,4
syscall
# output the number
li $v0,1
move $a0,$a1
syscall
# output a newline
la $a0,msg_nl
li $v0,4
syscall
jr $ra # return
If you'd like some tips on writing clean asm, based on my own experience, see my answer: MIPS linked list
I've used spim, QtSpim, and mars for simulators. Personally, I prefer mars where possible. See: http://courses.missouristate.edu/KenVollmar/mars/
How does 'alignment of memory operands' help MIPS to be pipelined?
The book says:
Fourth, as discussed in Chapter 2, operands must be aligned in memory. Hence,
we need not worry about a single data transfer instruction requiring two data
memory accesses; the requested data can be transferred between processor and
memory in a single pipeline stage.
I think I understand that one data transfer instruction does not require two or more data memory aaccesses.
However, I am not sure what does it have to do with the alignment of memory operands.
Thanks, in advance!
The lw instruction requires that the memory address be word aligned.
Therefore, to access an unaligned word, one would need to access the two word boundaries that the required word intersects and mask out the necessary bytes.
For example, suppose you desire to load a word stored at address 0x2. 0x2 is not word aligned, so you would need to load the half word stored at 0x2 and the half-word stored at 0x4.
To do so, one might write:
lh $t0 2($zero)
lh $t1 4($zero)
sll $t1 $t1 16
or $t2 $t0 $t1
This only gets more complicated if you want to load for example a word stored at address 0x3:
# load first byte
lb $t0 3($zero)
# load second word, mask out first 3 bytes
lw $t1 4($zero)
lui $t2 0x0000FFFF
ori $t2 $t2 0xFFFFFFFF
or $t1 $t1 $t2
# combine
sll $t1 $t1 8
or $t2 $t0 $t1
So, it can be seen that the requirement for word alignment doesn't help MIPS to be pipelined, but rather that access to unaligned words requires excess memory accesses — this is a limitation of the ISA.