confused over mips I-Type Instruction syntax
Sorry about this really stupid question. But I have been reading my textbook the entire day and its getting really confusing.
I would like to clarify some syntax related questions with MIPS I-type instructions.
I understand that instructions like
addi $21, $22, -50
are possible and the immediate value is interpreted as a decimal (2s complement).
But can I write addi $21, $22, 0xF1DF? such that the immediate value is in hexadecimal?
That brings me to my next question with andi $10, $11, 0xFFFF and ori $8, $0, 0x0FA5. I understand that these 2 codes are correct.
But can I write andi $10, $11, 120 and ori $8, $0, -16such that the immediate values are in decimal? If it is possible, does it interpret 120 as hexadecimal or does it convert decimal 120 to hexadecimal which equates to 78 in hexadecimal.
I am very confused.
mips mips32
add a comment |
Sorry about this really stupid question. But I have been reading my textbook the entire day and its getting really confusing.
I would like to clarify some syntax related questions with MIPS I-type instructions.
I understand that instructions like
addi $21, $22, -50
are possible and the immediate value is interpreted as a decimal (2s complement).
But can I write addi $21, $22, 0xF1DF? such that the immediate value is in hexadecimal?
That brings me to my next question with andi $10, $11, 0xFFFF and ori $8, $0, 0x0FA5. I understand that these 2 codes are correct.
But can I write andi $10, $11, 120 and ori $8, $0, -16such that the immediate values are in decimal? If it is possible, does it interpret 120 as hexadecimal or does it convert decimal 120 to hexadecimal which equates to 78 in hexadecimal.
I am very confused.
mips mips32
1
This is really a question about the assembler, not the processor. What assembler are you using?
– Raymond Chen
Nov 19 '18 at 19:19
@RaymondChen just normal QTSpim. I am also questioning if it is applicable in written exams
– deviljones
Nov 20 '18 at 3:26
Then read the documentation for that assembler. Page A-47, "Assembler syntax." ("Just normal QTSpim" makes it sound like "Well certainly everybody uses QTSpim as their assembler, right?" I don't think that's true. For education, MARS is also popular. And for professional use, people are going to use something other than a simulator.)
– Raymond Chen
Nov 20 '18 at 3:45
I have read the documentation, they do not explicitly say whether the immediate value is in decimal or hexadecimal form or could it be both?
– deviljones
Nov 20 '18 at 3:59
1
Quoting page A-47: "Numbers are base 10 by default. If they are preceded by 0x, they are interpreted as hexadecimal. Hence, 256 and 0x100 denote the same value."
– Raymond Chen
Nov 20 '18 at 4:43
add a comment |
Sorry about this really stupid question. But I have been reading my textbook the entire day and its getting really confusing.
I would like to clarify some syntax related questions with MIPS I-type instructions.
I understand that instructions like
addi $21, $22, -50
are possible and the immediate value is interpreted as a decimal (2s complement).
But can I write addi $21, $22, 0xF1DF? such that the immediate value is in hexadecimal?
That brings me to my next question with andi $10, $11, 0xFFFF and ori $8, $0, 0x0FA5. I understand that these 2 codes are correct.
But can I write andi $10, $11, 120 and ori $8, $0, -16such that the immediate values are in decimal? If it is possible, does it interpret 120 as hexadecimal or does it convert decimal 120 to hexadecimal which equates to 78 in hexadecimal.
I am very confused.
mips mips32
Sorry about this really stupid question. But I have been reading my textbook the entire day and its getting really confusing.
I would like to clarify some syntax related questions with MIPS I-type instructions.
I understand that instructions like
addi $21, $22, -50
are possible and the immediate value is interpreted as a decimal (2s complement).
But can I write addi $21, $22, 0xF1DF? such that the immediate value is in hexadecimal?
That brings me to my next question with andi $10, $11, 0xFFFF and ori $8, $0, 0x0FA5. I understand that these 2 codes are correct.
But can I write andi $10, $11, 120 and ori $8, $0, -16such that the immediate values are in decimal? If it is possible, does it interpret 120 as hexadecimal or does it convert decimal 120 to hexadecimal which equates to 78 in hexadecimal.
I am very confused.
mips mips32
mips mips32
asked Nov 19 '18 at 18:09
deviljonesdeviljones
425
425
1
This is really a question about the assembler, not the processor. What assembler are you using?
– Raymond Chen
Nov 19 '18 at 19:19
@RaymondChen just normal QTSpim. I am also questioning if it is applicable in written exams
– deviljones
Nov 20 '18 at 3:26
Then read the documentation for that assembler. Page A-47, "Assembler syntax." ("Just normal QTSpim" makes it sound like "Well certainly everybody uses QTSpim as their assembler, right?" I don't think that's true. For education, MARS is also popular. And for professional use, people are going to use something other than a simulator.)
– Raymond Chen
Nov 20 '18 at 3:45
I have read the documentation, they do not explicitly say whether the immediate value is in decimal or hexadecimal form or could it be both?
– deviljones
Nov 20 '18 at 3:59
1
Quoting page A-47: "Numbers are base 10 by default. If they are preceded by 0x, they are interpreted as hexadecimal. Hence, 256 and 0x100 denote the same value."
– Raymond Chen
Nov 20 '18 at 4:43
add a comment |
1
This is really a question about the assembler, not the processor. What assembler are you using?
– Raymond Chen
Nov 19 '18 at 19:19
@RaymondChen just normal QTSpim. I am also questioning if it is applicable in written exams
– deviljones
Nov 20 '18 at 3:26
Then read the documentation for that assembler. Page A-47, "Assembler syntax." ("Just normal QTSpim" makes it sound like "Well certainly everybody uses QTSpim as their assembler, right?" I don't think that's true. For education, MARS is also popular. And for professional use, people are going to use something other than a simulator.)
– Raymond Chen
Nov 20 '18 at 3:45
I have read the documentation, they do not explicitly say whether the immediate value is in decimal or hexadecimal form or could it be both?
– deviljones
Nov 20 '18 at 3:59
1
Quoting page A-47: "Numbers are base 10 by default. If they are preceded by 0x, they are interpreted as hexadecimal. Hence, 256 and 0x100 denote the same value."
– Raymond Chen
Nov 20 '18 at 4:43
1
1
This is really a question about the assembler, not the processor. What assembler are you using?
– Raymond Chen
Nov 19 '18 at 19:19
This is really a question about the assembler, not the processor. What assembler are you using?
– Raymond Chen
Nov 19 '18 at 19:19
@RaymondChen just normal QTSpim. I am also questioning if it is applicable in written exams
– deviljones
Nov 20 '18 at 3:26
@RaymondChen just normal QTSpim. I am also questioning if it is applicable in written exams
– deviljones
Nov 20 '18 at 3:26
Then read the documentation for that assembler. Page A-47, "Assembler syntax." ("Just normal QTSpim" makes it sound like "Well certainly everybody uses QTSpim as their assembler, right?" I don't think that's true. For education, MARS is also popular. And for professional use, people are going to use something other than a simulator.)
– Raymond Chen
Nov 20 '18 at 3:45
Then read the documentation for that assembler. Page A-47, "Assembler syntax." ("Just normal QTSpim" makes it sound like "Well certainly everybody uses QTSpim as their assembler, right?" I don't think that's true. For education, MARS is also popular. And for professional use, people are going to use something other than a simulator.)
– Raymond Chen
Nov 20 '18 at 3:45
I have read the documentation, they do not explicitly say whether the immediate value is in decimal or hexadecimal form or could it be both?
– deviljones
Nov 20 '18 at 3:59
I have read the documentation, they do not explicitly say whether the immediate value is in decimal or hexadecimal form or could it be both?
– deviljones
Nov 20 '18 at 3:59
1
1
Quoting page A-47: "Numbers are base 10 by default. If they are preceded by 0x, they are interpreted as hexadecimal. Hence, 256 and 0x100 denote the same value."
– Raymond Chen
Nov 20 '18 at 4:43
Quoting page A-47: "Numbers are base 10 by default. If they are preceded by 0x, they are interpreted as hexadecimal. Hence, 256 and 0x100 denote the same value."
– Raymond Chen
Nov 20 '18 at 4:43
add a comment |
2 Answers
2
active
oldest
votes
You can express immediates in both base 10 or hexadecimal. To express an hedacimal value you need to use the 0x prefix before the numeral (e.g. 0x10, which is 16 in base 10). If you don't use the prefix 0x before the numeral, the assembler will assume the numeral is in decimal notation.
Using your example, if you want to add 120 (base 10) you should encode the instruction like so addi $10, $11, 120 or like so addi $10, $11, 0x78. The assembler will produce the same binary instruction 0x20000000. You can try it for yourself here with this online assembler.
add a comment |
MIPS addi / addiu sign-extend the 16-bit immediate to 32 bits (2's complement):
Values from -32768 (0xFFFF8000) to 32767 (0x00007FFFF) are encodeable.
MIPS andi / ori zero-extend the 16-bit immediate to 32 bits,
Values from 0 (0x00000000) to 65535 (0x0000FFFFF) are encodeable.
So no, you can't write ori $8, $0, -16, you'll have to use addiu.
(Or inefficiently, lui $8, 0xFFFF / ori $8, $8, 0xfff0).
Usually you can leave this decision-making to the assembler, and let it choose how to create a constant with li $8, 0xFFFFFFF0 or li $8, -16. Those will assemble identically.
But if you're not just creating a constant, you do have to know what's encodeable. For example, on the Godbolt compiler explorer, gcc5.4 compiles these functions as follows for MIPS:
unsigned mask(unsigned a) {
return a & -16UL;
}
# not encodeable as an immediate for andi
li $2,-16 # 0xfffffffffffffff0
j $31
and $2,$4,$2
unsigned mask_positive(unsigned a) {
return a & 0xFF;
}
j $31
andi $2,$4,0x00ff # encodeable as an immediate
The representation in the source is totally irrelevant; hex and decimal are 2 different serialization formats for numbers. 0x10 and 16 are literally the same thing. In the MIPS machine code, they're stored in 16-bit binary, not hex or decimal
-32768 and 0xFFFF8000 are also the same thing (in 32-bit 2's complement). It's also 0x8000 in 16-bit 2's complement. And it's not representable as a 16-bit unsigned number, because 0x8000 zero-extends to 0x00008000 (+32768).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53380378%2fconfused-over-mips-i-type-instruction-syntax%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can express immediates in both base 10 or hexadecimal. To express an hedacimal value you need to use the 0x prefix before the numeral (e.g. 0x10, which is 16 in base 10). If you don't use the prefix 0x before the numeral, the assembler will assume the numeral is in decimal notation.
Using your example, if you want to add 120 (base 10) you should encode the instruction like so addi $10, $11, 120 or like so addi $10, $11, 0x78. The assembler will produce the same binary instruction 0x20000000. You can try it for yourself here with this online assembler.
add a comment |
You can express immediates in both base 10 or hexadecimal. To express an hedacimal value you need to use the 0x prefix before the numeral (e.g. 0x10, which is 16 in base 10). If you don't use the prefix 0x before the numeral, the assembler will assume the numeral is in decimal notation.
Using your example, if you want to add 120 (base 10) you should encode the instruction like so addi $10, $11, 120 or like so addi $10, $11, 0x78. The assembler will produce the same binary instruction 0x20000000. You can try it for yourself here with this online assembler.
add a comment |
You can express immediates in both base 10 or hexadecimal. To express an hedacimal value you need to use the 0x prefix before the numeral (e.g. 0x10, which is 16 in base 10). If you don't use the prefix 0x before the numeral, the assembler will assume the numeral is in decimal notation.
Using your example, if you want to add 120 (base 10) you should encode the instruction like so addi $10, $11, 120 or like so addi $10, $11, 0x78. The assembler will produce the same binary instruction 0x20000000. You can try it for yourself here with this online assembler.
You can express immediates in both base 10 or hexadecimal. To express an hedacimal value you need to use the 0x prefix before the numeral (e.g. 0x10, which is 16 in base 10). If you don't use the prefix 0x before the numeral, the assembler will assume the numeral is in decimal notation.
Using your example, if you want to add 120 (base 10) you should encode the instruction like so addi $10, $11, 120 or like so addi $10, $11, 0x78. The assembler will produce the same binary instruction 0x20000000. You can try it for yourself here with this online assembler.
edited Nov 21 '18 at 16:41
answered Nov 20 '18 at 18:34
rdarda
764
764
add a comment |
add a comment |
MIPS addi / addiu sign-extend the 16-bit immediate to 32 bits (2's complement):
Values from -32768 (0xFFFF8000) to 32767 (0x00007FFFF) are encodeable.
MIPS andi / ori zero-extend the 16-bit immediate to 32 bits,
Values from 0 (0x00000000) to 65535 (0x0000FFFFF) are encodeable.
So no, you can't write ori $8, $0, -16, you'll have to use addiu.
(Or inefficiently, lui $8, 0xFFFF / ori $8, $8, 0xfff0).
Usually you can leave this decision-making to the assembler, and let it choose how to create a constant with li $8, 0xFFFFFFF0 or li $8, -16. Those will assemble identically.
But if you're not just creating a constant, you do have to know what's encodeable. For example, on the Godbolt compiler explorer, gcc5.4 compiles these functions as follows for MIPS:
unsigned mask(unsigned a) {
return a & -16UL;
}
# not encodeable as an immediate for andi
li $2,-16 # 0xfffffffffffffff0
j $31
and $2,$4,$2
unsigned mask_positive(unsigned a) {
return a & 0xFF;
}
j $31
andi $2,$4,0x00ff # encodeable as an immediate
The representation in the source is totally irrelevant; hex and decimal are 2 different serialization formats for numbers. 0x10 and 16 are literally the same thing. In the MIPS machine code, they're stored in 16-bit binary, not hex or decimal
-32768 and 0xFFFF8000 are also the same thing (in 32-bit 2's complement). It's also 0x8000 in 16-bit 2's complement. And it's not representable as a 16-bit unsigned number, because 0x8000 zero-extends to 0x00008000 (+32768).
add a comment |
MIPS addi / addiu sign-extend the 16-bit immediate to 32 bits (2's complement):
Values from -32768 (0xFFFF8000) to 32767 (0x00007FFFF) are encodeable.
MIPS andi / ori zero-extend the 16-bit immediate to 32 bits,
Values from 0 (0x00000000) to 65535 (0x0000FFFFF) are encodeable.
So no, you can't write ori $8, $0, -16, you'll have to use addiu.
(Or inefficiently, lui $8, 0xFFFF / ori $8, $8, 0xfff0).
Usually you can leave this decision-making to the assembler, and let it choose how to create a constant with li $8, 0xFFFFFFF0 or li $8, -16. Those will assemble identically.
But if you're not just creating a constant, you do have to know what's encodeable. For example, on the Godbolt compiler explorer, gcc5.4 compiles these functions as follows for MIPS:
unsigned mask(unsigned a) {
return a & -16UL;
}
# not encodeable as an immediate for andi
li $2,-16 # 0xfffffffffffffff0
j $31
and $2,$4,$2
unsigned mask_positive(unsigned a) {
return a & 0xFF;
}
j $31
andi $2,$4,0x00ff # encodeable as an immediate
The representation in the source is totally irrelevant; hex and decimal are 2 different serialization formats for numbers. 0x10 and 16 are literally the same thing. In the MIPS machine code, they're stored in 16-bit binary, not hex or decimal
-32768 and 0xFFFF8000 are also the same thing (in 32-bit 2's complement). It's also 0x8000 in 16-bit 2's complement. And it's not representable as a 16-bit unsigned number, because 0x8000 zero-extends to 0x00008000 (+32768).
add a comment |
MIPS addi / addiu sign-extend the 16-bit immediate to 32 bits (2's complement):
Values from -32768 (0xFFFF8000) to 32767 (0x00007FFFF) are encodeable.
MIPS andi / ori zero-extend the 16-bit immediate to 32 bits,
Values from 0 (0x00000000) to 65535 (0x0000FFFFF) are encodeable.
So no, you can't write ori $8, $0, -16, you'll have to use addiu.
(Or inefficiently, lui $8, 0xFFFF / ori $8, $8, 0xfff0).
Usually you can leave this decision-making to the assembler, and let it choose how to create a constant with li $8, 0xFFFFFFF0 or li $8, -16. Those will assemble identically.
But if you're not just creating a constant, you do have to know what's encodeable. For example, on the Godbolt compiler explorer, gcc5.4 compiles these functions as follows for MIPS:
unsigned mask(unsigned a) {
return a & -16UL;
}
# not encodeable as an immediate for andi
li $2,-16 # 0xfffffffffffffff0
j $31
and $2,$4,$2
unsigned mask_positive(unsigned a) {
return a & 0xFF;
}
j $31
andi $2,$4,0x00ff # encodeable as an immediate
The representation in the source is totally irrelevant; hex and decimal are 2 different serialization formats for numbers. 0x10 and 16 are literally the same thing. In the MIPS machine code, they're stored in 16-bit binary, not hex or decimal
-32768 and 0xFFFF8000 are also the same thing (in 32-bit 2's complement). It's also 0x8000 in 16-bit 2's complement. And it's not representable as a 16-bit unsigned number, because 0x8000 zero-extends to 0x00008000 (+32768).
MIPS addi / addiu sign-extend the 16-bit immediate to 32 bits (2's complement):
Values from -32768 (0xFFFF8000) to 32767 (0x00007FFFF) are encodeable.
MIPS andi / ori zero-extend the 16-bit immediate to 32 bits,
Values from 0 (0x00000000) to 65535 (0x0000FFFFF) are encodeable.
So no, you can't write ori $8, $0, -16, you'll have to use addiu.
(Or inefficiently, lui $8, 0xFFFF / ori $8, $8, 0xfff0).
Usually you can leave this decision-making to the assembler, and let it choose how to create a constant with li $8, 0xFFFFFFF0 or li $8, -16. Those will assemble identically.
But if you're not just creating a constant, you do have to know what's encodeable. For example, on the Godbolt compiler explorer, gcc5.4 compiles these functions as follows for MIPS:
unsigned mask(unsigned a) {
return a & -16UL;
}
# not encodeable as an immediate for andi
li $2,-16 # 0xfffffffffffffff0
j $31
and $2,$4,$2
unsigned mask_positive(unsigned a) {
return a & 0xFF;
}
j $31
andi $2,$4,0x00ff # encodeable as an immediate
The representation in the source is totally irrelevant; hex and decimal are 2 different serialization formats for numbers. 0x10 and 16 are literally the same thing. In the MIPS machine code, they're stored in 16-bit binary, not hex or decimal
-32768 and 0xFFFF8000 are also the same thing (in 32-bit 2's complement). It's also 0x8000 in 16-bit 2's complement. And it's not representable as a 16-bit unsigned number, because 0x8000 zero-extends to 0x00008000 (+32768).
edited Nov 21 '18 at 3:42
answered Nov 21 '18 at 3:36
Peter CordesPeter Cordes
126k18189321
126k18189321
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53380378%2fconfused-over-mips-i-type-instruction-syntax%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
1
This is really a question about the assembler, not the processor. What assembler are you using?
– Raymond Chen
Nov 19 '18 at 19:19
@RaymondChen just normal QTSpim. I am also questioning if it is applicable in written exams
– deviljones
Nov 20 '18 at 3:26
Then read the documentation for that assembler. Page A-47, "Assembler syntax." ("Just normal QTSpim" makes it sound like "Well certainly everybody uses QTSpim as their assembler, right?" I don't think that's true. For education, MARS is also popular. And for professional use, people are going to use something other than a simulator.)
– Raymond Chen
Nov 20 '18 at 3:45
I have read the documentation, they do not explicitly say whether the immediate value is in decimal or hexadecimal form or could it be both?
– deviljones
Nov 20 '18 at 3:59
1
Quoting page A-47: "Numbers are base 10 by default. If they are preceded by 0x, they are interpreted as hexadecimal. Hence, 256 and 0x100 denote the same value."
– Raymond Chen
Nov 20 '18 at 4:43