I am a beginner trying to design an assembler for MIPS. Any help would be greatly appreciated.
Suppose I have this assembly file
main: lw $a0, 0($t0)
begin: addi $t0, $zero, 0 # beginning
addi $t1, $zero, 1
loop: slt $t2, $a0, $t1 # top of loop
bne $t2, $zero, finish
add $t0, $t0, $t1
addi $t1, $t1, 2
j loop # bottom of loop
finish: add $v0, $t0, $zero
I understand that the jump instruction follows the J-type instruction format as follows:
From the instruction format, I got that the target address would be a 26-bits memory address in binary. Please correct me if I am wrong anywhere until this point.
I managed to develop a program that reads the assembly file and pinpoints the labels (i.e. the labels "main" and "loop") and retrieves their corresponding memory addresses.
For example: for the label "loop," it is located at the memory address 00401595.
I was wondering if I could just simply convert that memory address into binary and append it to the opcode of the jump instruction, will I be able to get the correct machine code for the line:
j loop
Or is there another correct way of obtaining the target address for the J-type instruction format?
And also, my professor once told me that I could initialize the program counter to exactly 0. So, I assume that the memory address for "main" would be 0? Is there a way I could do this manually?
Thanks in advance.