Results 1 to 8 of 8

Thread: How do u figure out the 16 bit offset of a branch? [Resovled]

  1. #1

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    How do u figure out the 16 bit offset of a branch? [Resovled]

    Hello everyone.

    I'm studying for my exam and i'm stuck on this problem..

    For the asm code given below, what is the value of the 16-bit offset in the instruction 'beq'
    beq $t0, $t1, label
    sub $t2, $t0, $t1
    addi $t2, $t2, 4
    sw $t2, 0($s0)
    label:.....

    the answer is 0x0010, I thought u start counting from the beq instruction but yesterday the profesor showed me on the sample test and she said u don't start counting at beq, but right after beq, so u would count down till u get to the label:...


    This would make sense to me if u start counting at beq, then it would be
    0
    4
    8
    12
    16

    if u convert 16 from decimal to hex u get 10, so is that how they got 0x0010 ?


    But the professor explained this problem to me which doesn't follow that pattern:

    loop: beq $s0, $s2, exit
    add $s0, $s0, $s1
    j loop
    exit: lui $s0, 48

    whats the machine code?
    answer:
    beq [4][16][18][2]

    2 is the 16 bit offest in this case, i asked her how did u get 2?
    well she said, u start one right after the branch instruction and start counting...
    so if u did that, u would count
    0
    4
    8 and at 8, u would hit the label exit:
    she said they got 2 because ur jumping 2 words, so its 2.

    can anyone help me out?
    Last edited by voidflux; Oct 9th, 2006 at 12:35 PM.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How do u figure out the 16 bit offset of a branch?

    Well, it's exactly as she said. The byte offset is the number of instructions * 4. But in the machine code, the word offset (i.e. the instructoin count) is stored, not the byte offset.

    Be glad you've got a fixed-size instruction set here
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Re: How do u figure out the 16 bit offset of a branch?

    Thanks for the responce!

    So for
    For the asm code given below, what is the value of the 16-bit offset in the instruction 'beq'
    beq $t0, $t1, label
    sub $t2, $t0, $t1
    addi $t2, $t2, 4
    sw $t2, 0($s0)
    label:.....

    So if she wants the 16 bit offset, and the chocies are the following:
    A. 0x0010
    B. 0x0004
    C. 0x0003
    D. 0x000c

    when u say instruction count do u mean, like:

    0 beq $t0, $t1, label
    1 sub $t2, $t0, $t1
    2 addi $t2, $t2, 4
    3 sw $t2, 0($s0)
    4 label:.....

    so if u start counting at Program counter + 4, meaning the line right after you encounter the beq, you would get, 4 words away until you hit label:...
    but I don't see how that equals 0x0010.

    If you break it into just looking at the words you would count by 4's and get:
    0 beq $t0, $t1, label
    4 sub $t2, $t0, $t1
    8 addi $t2, $t2, 4
    12 sw $t2, 0($s0)
    16 label:.....

    and if u started counting at sub $t2 by 4's you would get, 12 until you hit label:....

    thanks!
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How do u figure out the 16 bit offset of a branch?

    You're still counting the branch instruction itself. Don't. From the beq to the label it's three instructions (sub, addi, sw), i.e. three words, 12 (0xC) bytes.

    If you want to know which you have to specify at the test, you'd best ask her.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Re: How do u figure out the 16 bit offset of a branch?

    Ahh i got you now! its 3 words like you said or 3*4 = 12 and in hex it would be 0xC. But on the quiz the answer was 0x0010 but there was a choiice of 0x000C, do you think there was an error on the quiz (there are lots) and the answer should have been 0x000C?
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How do u figure out the 16 bit offset of a branch?

    From all you've said, I'm pretty sure.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Re: How do u figure out the 16 bit offset of a branch?

    Thanks! i'll ask her about it tomarrow
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  8. #8

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Re: How do u figure out the 16 bit offset of a branch? [Resovled]

    She said hte correct answer is:
    C. 0x0003
    because its 3 "words" away
    or u can write out the binary because its 4*3 bytes away, and chop of the lower 2 order bits and also the high order 4 bits and then regroup and you'll come out with x0003
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width