Is there a way to make MARS or RARS disassemble an instruction given in hex? - mips

MARS & RARS contain a disassembler, but
don't allow .word within .text
will only disassemble the .text section
Is there a way to get these simulators to disassemble an instruction from hex?
(The common online disassemblers also don't support RISC V!)

The following code sequence will make RARS/MARS disassemble from hex (RARS version here).  The program can be edited to use other instructions as hex, and, after running the program, disassembly can be seen in the "Text Segment" column "Basic".  The option for "Self-modifying code" must be enabled in the "Settings" menu.
.data
WStart:
.word 0x00052283 # as many instructions in hex or other here as will fit in the nop's below
.word 0xfae7d2e3
WEnd:
.text
main:
j next
CC0: # after running the program,
nop # find disassembly here in the "Basic" column of the "Text Segment" window
nop
nop
nop
nop
nop
nop
nop
nop
next:
la a0, WStart
la a1, WEnd
la a2, CC0
loop1:
lw t0, (a0)
sw t0, (a2)
addi a0, a0, 4
addi a2, a2, 4
bne a0, a1, loop1
li a7, 10
ecall

Related

calculate target address for mips jalr

I have the following assembly code disassembled using capstone. I started with entry point obtained from header.
.text : 4195648
...
...
0x400584L lui $t9, 0x40
0x400588L addiu $t9, $t9, 0xba0
0x40058cL jalr $t9
How do I find which address jalr is pointing to? (I thought I could reach main function in C program)
Based on slide number 19 in this reference I made target = 0x0400ba0
I looked for it and found it in .plt section. Here is the disassembly of .plt
.plt : 4197152
...
...
0x400ba0L lui $t7, 0x41
0x400ba4L lw $t9, 0xbe4($t7)
0x400ba8L jr $t9
0x400bacL addiu $t8, $t7, 0xbe4
I am confused - How do I make sense of it. Or how can I reach actual main function in the MIPS disassembly

MIPS: Is it possible to print the value that is inside storage that contains .word?

i am currently learning MIPS and i have a question about MIPS. As the question title, is it possible to print a value that is inside a storage that declared with ".word"
EX:
.data
var1: .word 3
.text
.globl main
main:
li $v0, 4
la $a0, var1
syscall
So is it possible for it to printed about the value 3 from var1?
Sure, just load the word from that address:
la $a0, var1
lw $a0, ($a0)
Or, if you wanted to, you could replace those two instructions with a single lw:
lw $a0, var1
Note that this is a pseudo-instruction, which the assembler will translate into 1 or more actual instructions. So you're not gaining anything at the machine code level, but your assembly code will be more compact.

Using lw on address of $ra

Assuming we have something of this sort in MIPS
jal F
F: lw $t1, 0($ra)
lw $t2, 8($ra)
What would we have in $t1 and $t2?
Will $t1 have the instruction code of F??
Say X is the address of label F
when jal is called $ra will hold X
you load what is on $ra to $t1 which is the binary machine laguage of the instruction at X
then you load what is 2 words lower (8) which is here not specified in your code, you need to show a 4th line of code to tell you =)

MIPS How to branch to a 32-bit address?

I am trying to branch to an address:
bne $t0, $0, 0x7813a21c
However, this is incorrect because bne only allocates 16-bits to the immediate
How can I branch to a direct 32-bit address? Is there a way to branch from a value in a register?
You have to use JR to jump to an address stored in a register.
To preform this type of operation you will need a jump statement. You have to tell the code to jump control context to the exact line you wish to specify. This is example syntax: j offset Where in your address is the offset.
Here is a link that better reviews what you have to do. Check out the section on jump. These are the types of jump available. One of them is what you need: j offset, jal offset, jr $rs, jalr $rs
Here is the link:
http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/jump.html
Good luck
We can load 32-bit addresss to the register (e.g. $t1) in 2 steps:
Load the upper 16 bits by lui (Load Upper Immediate).
Load the lower 16 bits by ori (Or Immediate).
NOTE: It is work because lui fills the lower 16 bits with 0s, so bitwise OR load the lower 16 bits (n | 0 = n);
In code below if $t0 is equal to 0 we do skip jr instruction.
Or if $t0 is not equal to 0 we do not skip jr instruction (or we do jump).
beq $t0, $0, SKIP
# load 0x7813a21c to $t0
lui $t1, 0x7813 # load the upper 16 bits
# Now $t1 = 0x78130000
ori $t1, $1, 0xa21c # load the lower 16 bits
# Now $t1 = 0x7813A21C
jr $t1 # as #Matt Eckert said
SKIP:

the functions (procedures) in MIPS

I'm new in MIPS language and I don't understand how the functions (procedures) in the MIPS assembly language work. Here are but I will specify my problem :
What does:
jal
jr
$ra
mean in mips language and the important thing
How can we use them when we want to create a function or (procedure)?
Firstly, you might want to check this quick MIPS reference. It really helped me.
Secondly, to explain jal, jr and $ra. What jal <label> does is jump to the label label and store the program counter (think of it as the address of the current instruction) in the $ra register. Now, when you want to return from label to where you initially were, you just use jr $ra.
Here's an example:
.text
main:
li $t0, 1
jal procedure # call procedure
li $v0, 10
syscall
procedure:
li $t0, 3
jr $ra # return
You will notice when running this in a SPIM emulator that the value left in $t0 is 3, the one loaded in the so-called procedure.
Hope this helps.
1.the first two are instructions,the third it's kind of special register
jal=jump and link (Address of following instruction put in $ra,and jump to target address)
jr=jump to specify register
$ra=return address
we often use the instruction like this ...
jr $ra (Copy $ra to program counter)
it means return(jump) to the address saved in $ra .
2.
Here's an example function (procedure) in C
int main(){
x=addthem(a,b);
}
int addthem(int a, int b){
return a+b;
}
function in MIPS
.text
main: #assume value a is already in $t0, b in $t1
add $a0,$0,$t0 # it's the same function as move the value
add $a1,$0,$t1
jal addthem # call procedure
add $t3,$0,$v0 # move the return value from $v0 to where we want
syscall
addthem:
addi $sp,$sp,-4 # Moving Stack pointer
sw $t0, 0($sp) # Store previous value
add $t0,$a0,$a1 # Procedure Body
add $v0,$0,$t0 # Result
lw $t0, 0($sp) # Load previous value
addi $sp,$sp,4 # Moving Stack pointer
jr $ra # return (Copy $ra to PC)
You will want to read the System V Application Binary Interface, MIPS RISC Processor Supplement. This describes the conventions used for calling functions, in particular how the stack is managed and parameters are exchanged (there is no hardware stack in MIPS, everything is a matter of software conventions, and the ABI defines those conventions).
The document above assumes some basic knowledge of what MIPS instructions do, so you will also need the MIPS32 Architecture for Programmers, in particular volume II (instruction set), which describes the detailed effect of each instruction. But, do yourself a favor, download and read volume I (introduction) first.
The jal instruction is the "jump and link" opcode. It jumps at the target address (which is the address of the first opcode of the called procedure) while saving the current instruction pointer into the link register, which is register 31 (to be precise, it saves in register 31 the value x+8, where x is the address of the jal opcode itself).
jal: aka jump and link against any function name will redirect you to the required function.
jr $ra: It returns the value from a function that was called.
main function:
.data
x: .word 3 # initializing x to be 3
y: .word 5 # initializing y to be 5
answer: .word 0 # intialzing answer to be 0
prompt: .asciiz "Add X to Y: " # console prompt for final answer
.text
.globl main
.ent main
main:
lw $a0, x # pass arguments to function $a0 - x, $a1 - y
lw $a1, y
jal adds # adds function is called
sw $v0, answer # answer from the returned value
la $a0, prompt # displaying prompt on the console
li $v0, 4
syscall
lw $a0, answer # displaying final answer on the console
li $v0, 1
syscall
li $v0, 10 # end program
syscall
.end main
adds function:
.globl adds
.ent adds
adds: # adds function
li $v0, 0
add $v0, $a0, $a1 # adding arguments to the callee
jr $ra # return
.end adds