Trouble with Jump Offset

172 views
Skip to first unread message

Trevor Bonas

unread,
Oct 3, 2020, 6:30:55 PM10/3/20
to cmpt-295-sfu
Github username: trevorbonas
Github: Link to github repo (your assignment repos are private so these links are only accessible to TAs and Instructor) g...@github.com:CMPT-295-SFU/assignment-2-trevorbonas.git
Line and file: Line 140 utils.c
Expected behavior: Running make part1:
00001020: jal x0, -12
00001050: jal x0, 8
Observed behavior: Running make part1
00001020: jal x0, -2561
00001050: jal x0, 2048
Question: From reading others' questions I think my get_jump_offset function in utils.c isn't implemented correctly, but I honestly don't understand why the function is needed or how it would be implemented. I'm thinking that jal takes a destination register, in this case x0, and stores the current instruction's address there and then jumps to label (i.e., the address stored in instruction.ujtype.imm). The label is 20 bits long, I sign extended it so that it's a whole integer I can work with but this isn't enough. What steps should I take? 

Arrvindh Shriraman

unread,
Oct 3, 2020, 7:31:08 PM10/3/20
to cmpt-295-sfu

JAL is 

rd=PC+4;PC+=imm (RISCV Card)


jal x0, -12


In this case rd is x0 (or zero), so writing to it has no effect. 
As for the offset it is going to be updating PC.

Look at the format of the J-type instruction

imm[20|10:1|11|19:12]


The bits for the immediate are permuted (that is they are switched around). 

You need to reorganize the bits to become a 20 bit immediate (get_jump_offset() does that)

e.g., Bits 19---11 in the imm really correspond to bits 10:1 in the offset.


https://youtu.be/5bXyaWW-LjA?t=747




Trevor Bonas

unread,
Oct 3, 2020, 10:58:11 PM10/3/20
to cmpt-295-sfu
Thank you for the detailed reply. It feels like the RISC-V engineers were trying to make an insane puzzle. I pushed an attempt at getting the proper jump offset but I'm off by half (expected jal x0, 8 got jal x0, 4). I've tried many different shifts and masks and I still don't know what I'm doing wrong.

Arrvindh Shriraman

unread,
Oct 4, 2020, 1:14:32 AM10/4/20
to cmpt-295-sfu
your shifts and masks are off by 1 or 2 bits.
You are either shifting too much or your hex codes are off.
You can try this online converter to convert binary to hex: https://www.rapidtables.com/convert/number/binary-to-hex.html

Trevor Bonas

unread,
Oct 4, 2020, 5:50:47 AM10/4/20
to cmpt-295-sfu
I analyzed my masks and shifts ad nauseam and used the converter but the test still failed. I don't want to keep bothering you with the same problem but my last effort was to write out everything on paper and it may show there's a mistake in the test itself, or I really don't know what I'm doing. Attached is my drawing. The masks and the "offset |=" are left out but are in my code.


Screen Shot 2020-10-04 at 2.36.57 AM.png
Reply all
Reply to author
Forward
0 new messages