MIPS Need help in Calculating Branch Target Address

This topic contains 2 replies, has 2 voices, and was last updated by  Sean 4 years, 2 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #31435

    amir734jj
    Member

    I’m really new in MIPS. Here is a sample code I have written in MIPS to learn how to calculate branch target address. I assembled the code in Mars 4.3 but I don’t understand how Mars 4.3 found these : 0x00000003 and 0xfffffff4 values for branch.

    .data
    read_num_p: .asciiz “Num”
    .text
    read_num_loop: li $v0, 4 #prompt for number and read input
    la $a0, read_num_p
    syscall

    li $v0, 5
    syscall
    la $t9,read_num_end
    beqz $v0, read_num_end #test for exit
    add $t0, $t0, $v0 #add num to total & incr counter
    addi $t1, $t1, -1
    b read_num_loop

    read_num_end:
    move $v0, $t0 #Load values to be returned

    #37882

    amir734jj
    Member

    Here is the link for my code

    #37883

    Sean
    Moderator

    These values are being given as relative addresses from the PC (once the PC has moved on) and the numbers are word offsets. All instructions are on 32 bit boundaries so there is no need to give the bottom 2 bits in the offset.

    0x00400020 beq $2,$0,0x00000003
    0x00400024 + 3 words = 0x00400030

    0x0040002c bgez $0,0xfffffff4
    0x00400030 + -12 words = 0x00400000

    0xfff4 is -12 in 2s complement notation.

    I trust this clarifies what you are seeing in Mars?

    Sean.

Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic.