MIPS jump and BNE address calculation
up vote
1
down vote
favorite
Suppose the program counter (PC) is set to 0x20000000.Is it possible to use the jump (j) MIPS assembly instruction to set the PC to the address 0x40000000? Is it possible to use the branch-on-equal (beq) MIPS assembly instruction to set the PC to this same address?
mips cpu-architecture mips32
New contributor
add a comment |
up vote
1
down vote
favorite
Suppose the program counter (PC) is set to 0x20000000.Is it possible to use the jump (j) MIPS assembly instruction to set the PC to the address 0x40000000? Is it possible to use the branch-on-equal (beq) MIPS assembly instruction to set the PC to this same address?
mips cpu-architecture mips32
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Suppose the program counter (PC) is set to 0x20000000.Is it possible to use the jump (j) MIPS assembly instruction to set the PC to the address 0x40000000? Is it possible to use the branch-on-equal (beq) MIPS assembly instruction to set the PC to this same address?
mips cpu-architecture mips32
New contributor
Suppose the program counter (PC) is set to 0x20000000.Is it possible to use the jump (j) MIPS assembly instruction to set the PC to the address 0x40000000? Is it possible to use the branch-on-equal (beq) MIPS assembly instruction to set the PC to this same address?
mips cpu-architecture mips32
mips cpu-architecture mips32
New contributor
New contributor
edited Nov 21 at 16:50
Peter Cordes
116k16176302
116k16176302
New contributor
asked Nov 21 at 13:44
Hasin Sadique
61
61
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
I believe you could use jr
to jump to a 32bit address
li $t0, 0x40000000
jr $t0
Similarly you could use a branch and combine it with the above to perform this jump.
The reason why BNE
is not suited for this is that it only operates on 16-bit offsets due to how instructions are encoded.
0001 01ss ssst tttt iiii iiii iiii iiii
represents a BNE
instruction where sssss
and ttttt
are the compared registers and iiii iiii iiii iiii
the 16-bit offset (twos-complement to allow backwards offset as well).
This means that the 0x20000000
offset cannot be expressed in the 16-bits provided by this encoding.
The jump instruction behaves a bit differently in the way it uses the current PC to calculate the destination address. This is done by concatenating the first 6 bits of the current PC (which would be 0010 00
in this case) together with the destination stored in the immediate part of the encoding (which would be 26-bits filled with 0
). Thus the resulting address can only be 0x20000000
.
Jump-Register (JR
) on the other hand allows to jump to full 32-bit addresses since it uses a register for the destination address and is not bound by the aforementioned instruction limitations.
New contributor
1
Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
– Jevgeni Geurtsen
Nov 21 at 14:16
1
Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
– Minn
Nov 21 at 17:11
1
Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
– Jevgeni Geurtsen
Nov 21 at 18:21
add a comment |
up vote
1
down vote
The documentation states the following in the programming notes section of the BEQ
-instruction:
Description: if rs = rt then branch An 18-bit signed offset (the
16-bit offset field shifted left 2 bits) is added to the address of
the instruction following the branch (not the branch itself), in the
branch delay slot, to form a PC-relative effective target address.
Programming Notes:
With the 18-bit signed instruction offset, the
conditional branch range is ± 128 Kbytes. Use jump (J) or jump
register (JR) instructions to branch to addresses outside this range.
So you cannot use the BEQ
instruction to jump to 0x40000000
(starting from 0x20000000
it would have to jump over 0x20000000
= 536870912
b = 536871
kb). Thus you must use the jump register instructions to jump to 0x40000000
.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
I believe you could use jr
to jump to a 32bit address
li $t0, 0x40000000
jr $t0
Similarly you could use a branch and combine it with the above to perform this jump.
The reason why BNE
is not suited for this is that it only operates on 16-bit offsets due to how instructions are encoded.
0001 01ss ssst tttt iiii iiii iiii iiii
represents a BNE
instruction where sssss
and ttttt
are the compared registers and iiii iiii iiii iiii
the 16-bit offset (twos-complement to allow backwards offset as well).
This means that the 0x20000000
offset cannot be expressed in the 16-bits provided by this encoding.
The jump instruction behaves a bit differently in the way it uses the current PC to calculate the destination address. This is done by concatenating the first 6 bits of the current PC (which would be 0010 00
in this case) together with the destination stored in the immediate part of the encoding (which would be 26-bits filled with 0
). Thus the resulting address can only be 0x20000000
.
Jump-Register (JR
) on the other hand allows to jump to full 32-bit addresses since it uses a register for the destination address and is not bound by the aforementioned instruction limitations.
New contributor
1
Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
– Jevgeni Geurtsen
Nov 21 at 14:16
1
Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
– Minn
Nov 21 at 17:11
1
Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
– Jevgeni Geurtsen
Nov 21 at 18:21
add a comment |
up vote
3
down vote
I believe you could use jr
to jump to a 32bit address
li $t0, 0x40000000
jr $t0
Similarly you could use a branch and combine it with the above to perform this jump.
The reason why BNE
is not suited for this is that it only operates on 16-bit offsets due to how instructions are encoded.
0001 01ss ssst tttt iiii iiii iiii iiii
represents a BNE
instruction where sssss
and ttttt
are the compared registers and iiii iiii iiii iiii
the 16-bit offset (twos-complement to allow backwards offset as well).
This means that the 0x20000000
offset cannot be expressed in the 16-bits provided by this encoding.
The jump instruction behaves a bit differently in the way it uses the current PC to calculate the destination address. This is done by concatenating the first 6 bits of the current PC (which would be 0010 00
in this case) together with the destination stored in the immediate part of the encoding (which would be 26-bits filled with 0
). Thus the resulting address can only be 0x20000000
.
Jump-Register (JR
) on the other hand allows to jump to full 32-bit addresses since it uses a register for the destination address and is not bound by the aforementioned instruction limitations.
New contributor
1
Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
– Jevgeni Geurtsen
Nov 21 at 14:16
1
Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
– Minn
Nov 21 at 17:11
1
Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
– Jevgeni Geurtsen
Nov 21 at 18:21
add a comment |
up vote
3
down vote
up vote
3
down vote
I believe you could use jr
to jump to a 32bit address
li $t0, 0x40000000
jr $t0
Similarly you could use a branch and combine it with the above to perform this jump.
The reason why BNE
is not suited for this is that it only operates on 16-bit offsets due to how instructions are encoded.
0001 01ss ssst tttt iiii iiii iiii iiii
represents a BNE
instruction where sssss
and ttttt
are the compared registers and iiii iiii iiii iiii
the 16-bit offset (twos-complement to allow backwards offset as well).
This means that the 0x20000000
offset cannot be expressed in the 16-bits provided by this encoding.
The jump instruction behaves a bit differently in the way it uses the current PC to calculate the destination address. This is done by concatenating the first 6 bits of the current PC (which would be 0010 00
in this case) together with the destination stored in the immediate part of the encoding (which would be 26-bits filled with 0
). Thus the resulting address can only be 0x20000000
.
Jump-Register (JR
) on the other hand allows to jump to full 32-bit addresses since it uses a register for the destination address and is not bound by the aforementioned instruction limitations.
New contributor
I believe you could use jr
to jump to a 32bit address
li $t0, 0x40000000
jr $t0
Similarly you could use a branch and combine it with the above to perform this jump.
The reason why BNE
is not suited for this is that it only operates on 16-bit offsets due to how instructions are encoded.
0001 01ss ssst tttt iiii iiii iiii iiii
represents a BNE
instruction where sssss
and ttttt
are the compared registers and iiii iiii iiii iiii
the 16-bit offset (twos-complement to allow backwards offset as well).
This means that the 0x20000000
offset cannot be expressed in the 16-bits provided by this encoding.
The jump instruction behaves a bit differently in the way it uses the current PC to calculate the destination address. This is done by concatenating the first 6 bits of the current PC (which would be 0010 00
in this case) together with the destination stored in the immediate part of the encoding (which would be 26-bits filled with 0
). Thus the resulting address can only be 0x20000000
.
Jump-Register (JR
) on the other hand allows to jump to full 32-bit addresses since it uses a register for the destination address and is not bound by the aforementioned instruction limitations.
New contributor
edited Nov 21 at 17:57
New contributor
answered Nov 21 at 13:49
Minn
948
948
New contributor
New contributor
1
Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
– Jevgeni Geurtsen
Nov 21 at 14:16
1
Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
– Minn
Nov 21 at 17:11
1
Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
– Jevgeni Geurtsen
Nov 21 at 18:21
add a comment |
1
Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
– Jevgeni Geurtsen
Nov 21 at 14:16
1
Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
– Minn
Nov 21 at 17:11
1
Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
– Jevgeni Geurtsen
Nov 21 at 18:21
1
1
Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
– Jevgeni Geurtsen
Nov 21 at 14:16
Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
– Jevgeni Geurtsen
Nov 21 at 14:16
1
1
Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
– Minn
Nov 21 at 17:11
Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
– Minn
Nov 21 at 17:11
1
1
Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
– Jevgeni Geurtsen
Nov 21 at 18:21
Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
– Jevgeni Geurtsen
Nov 21 at 18:21
add a comment |
up vote
1
down vote
The documentation states the following in the programming notes section of the BEQ
-instruction:
Description: if rs = rt then branch An 18-bit signed offset (the
16-bit offset field shifted left 2 bits) is added to the address of
the instruction following the branch (not the branch itself), in the
branch delay slot, to form a PC-relative effective target address.
Programming Notes:
With the 18-bit signed instruction offset, the
conditional branch range is ± 128 Kbytes. Use jump (J) or jump
register (JR) instructions to branch to addresses outside this range.
So you cannot use the BEQ
instruction to jump to 0x40000000
(starting from 0x20000000
it would have to jump over 0x20000000
= 536870912
b = 536871
kb). Thus you must use the jump register instructions to jump to 0x40000000
.
add a comment |
up vote
1
down vote
The documentation states the following in the programming notes section of the BEQ
-instruction:
Description: if rs = rt then branch An 18-bit signed offset (the
16-bit offset field shifted left 2 bits) is added to the address of
the instruction following the branch (not the branch itself), in the
branch delay slot, to form a PC-relative effective target address.
Programming Notes:
With the 18-bit signed instruction offset, the
conditional branch range is ± 128 Kbytes. Use jump (J) or jump
register (JR) instructions to branch to addresses outside this range.
So you cannot use the BEQ
instruction to jump to 0x40000000
(starting from 0x20000000
it would have to jump over 0x20000000
= 536870912
b = 536871
kb). Thus you must use the jump register instructions to jump to 0x40000000
.
add a comment |
up vote
1
down vote
up vote
1
down vote
The documentation states the following in the programming notes section of the BEQ
-instruction:
Description: if rs = rt then branch An 18-bit signed offset (the
16-bit offset field shifted left 2 bits) is added to the address of
the instruction following the branch (not the branch itself), in the
branch delay slot, to form a PC-relative effective target address.
Programming Notes:
With the 18-bit signed instruction offset, the
conditional branch range is ± 128 Kbytes. Use jump (J) or jump
register (JR) instructions to branch to addresses outside this range.
So you cannot use the BEQ
instruction to jump to 0x40000000
(starting from 0x20000000
it would have to jump over 0x20000000
= 536870912
b = 536871
kb). Thus you must use the jump register instructions to jump to 0x40000000
.
The documentation states the following in the programming notes section of the BEQ
-instruction:
Description: if rs = rt then branch An 18-bit signed offset (the
16-bit offset field shifted left 2 bits) is added to the address of
the instruction following the branch (not the branch itself), in the
branch delay slot, to form a PC-relative effective target address.
Programming Notes:
With the 18-bit signed instruction offset, the
conditional branch range is ± 128 Kbytes. Use jump (J) or jump
register (JR) instructions to branch to addresses outside this range.
So you cannot use the BEQ
instruction to jump to 0x40000000
(starting from 0x20000000
it would have to jump over 0x20000000
= 536870912
b = 536871
kb). Thus you must use the jump register instructions to jump to 0x40000000
.
answered Nov 21 at 13:57
Jevgeni Geurtsen
2,54641231
2,54641231
add a comment |
add a comment |
Hasin Sadique is a new contributor. Be nice, and check out our Code of Conduct.
Hasin Sadique is a new contributor. Be nice, and check out our Code of Conduct.
Hasin Sadique is a new contributor. Be nice, and check out our Code of Conduct.
Hasin Sadique is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413461%2fmips-jump-and-bne-address-calculation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown