Class 3.23 MIPS Assembly Syntax. Example Programx
Download
Report
Transcript Class 3.23 MIPS Assembly Syntax. Example Programx
MIPS assembly syntax
Comments
The character "#" (sharp sign) starts a comment;
Everything on the line from "#" to the right is ignored.
Sometimes you can use two or more in a row for
emphasis, but only one is needed.
## Program to add two plus three -
is a
comment. It is ignored by the assembler and results in no
machine instructions.
Identifiers
Identifiers are a sequence of alphanumeric characters,
underbars (_), and dots (.) that do not begin with a number.
Directives
A directive is a statement that tells the assembler
something about what the programmer wants, but does not
itself result in any machine instructions.
.text
Subsequent items put in user text segment
(machine code)
.data
Subsequent items put in user data segment
(binary rep of data in source file)
.globl sym
Declares sym global and can be
referenced from other files
.asciiz str
Store the string str in memory and nullterminate it
.byte b1…bn Store the n values in successive
memory bytes.
.word w1…wn Store the n 32-bit quantities in
successive memory words
An example program
.text is a directive.
This directive tells the assembler that
• the following lines are instructions
• and should produce machine instructions
• and put into the binary code text segment.
For simple programs
.text and .globl could
be omitted.
.globl main is another directive.
## Program to add two plus
three
It says
that
.text
• the identifier main will be used outside of this
source file (that is, used "globally")
.globl main
main:
ori $8, $0, 0x2
ori $9, $0, 3
add $10, $8, $9
## End of file
• as the label of a particular location in main
memory.
# put the number two into register 8
# put the number three into register 9
# add register 8 and 9, put result in 10
The blank line is ignored.
Labels
Labels are declared by putting them at the beginning of a line followed by
a colon, for example:
main:
ori
$8,$0,0x2
main: symbolic address or a statement label.
A symbolic address is a symbol (an identifier followed by a colon ``:'') that is
the source code name for a location in memory.
In this program, main stands for the address of the first machine
instruction (which turns out to be 0x00400000).
## Program to add two plus three
.text
.globl main
main:
ori $8, $0, 0x2
ori $9, $0, 3
add $10, $8, $9
## End of file
# put the number two into register 8
# put the number three into register 9
# add register 8 and 9, put result in 10
Opcodes
Instruction opcodes are reserved words that cannot be used
as identifiers
ori, add – are operation codes (opcodes) of assembly
instructions.
.text
.globl main
main:
ori $8, $0, 0x2
ori $9, $0, 3
add $10, $8, $9
# put the number two into register 8
# put the number three into register 9
# add register 8 and 9, put result in 10
Machine Instructions
• Each of the three lines following main: corresponds to one machine
instruction.
• The line: ori $8,$0,0x2 is translated into a 32-bit machine
instruction.
• The machine instruction, upon execution, puts a 32-bit two's
complement positive 2 into register 8(details later).
• The instruction after that is translated into a machine instruction that
(upon execution) puts a 3 into register 9.
• The final instruction is translated into a machine instruction that (upon
execution) adds register 8 to register 9 and puts the 32-bit result into
register 10.
.text
.globl main
main:
ori $8, $0, 0x2
ori $9, $0, 3
add $10, $8, $9
# put the number two into register 8
# put the number three into register 9
# add register 8 and 9, put result in 10
Numbers
• Numbers are base 10 by default.
• If they are preceded by 0x, they are interpreted as
hexadecimal.
• 256 and 0x100 denote the same value.
ori $8,$0,0x100
ori $8,$0,256
.text
.globl main
main:
ori $8, $0, 0x2
ori $9, $0, 3
add $10, $8, $9
# put the number two into register 8
# put the number three into register 9
# add register 8 and 9, put result in 10
Strings, Data
Strings are enclosed in double quotes (").
.data
string: .asciiz
.asciiz str
"Hello SPIM!\n\n"
- Stores the string str in memory and null-terminates it.
Special characters in strings follow the C convention:
newline \n
tab \t
quote \"
.data is a directive. This directive tells the assembler that
the subsequent items should be stored in the data segment of
binary code.
Examples on the AUA server