“This is the 22nd day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

Introduction to the

We know that x86 architecture CPU is used in PC and workstation more,ARM architecture CPU is commonly found in mobile phones and microcontrollers, then MIPS architecture CPU mainly in which devices can be found their figure?

  • In China the godson
  • playstation

Learning environment building

  • Install JDK, mainly used to run MIPS emulator Mars
  • MARS simulator: courses.missouristate.edu/KenVollmar/…

register

In MIPS, there are 32 universal registers starting with $

| | | register number register name register purpose | | : — — — — — — — — — — : | : — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — : | : — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — : | | | 0 ∣ 0 0 ∣ zero | always returns 0 | | 1 ∣ | 1 ∣ | at | keep register | 2-2-2-3 | where v0 – where v0 – where v0 – v1 | generally used to store the expression or function return value (the value of the abbreviated) | | 4-4-4-7 | a0 – a0 – a0 – a3 (Argument () | | parameter register | 8-8-8-15 | t0 – t0 – t0 – t7 has | Commonly used to store temporary variables (temp abbreviated) | | 16-16-16-23 | s0 – s0 – s0 deposit – s7 | sub function call process data need to be retained (saved values) | | 24-24-24-25 | t8 – t8 – t8 – t9 with | attributes T0 – t0 – t0 – t7 has | | – 26 of 26-26-27 | k0 – k0 – k0 – k1 general storage interrupt function return value | | | 28 ∣ 28 28 ∣ gp | | GlobalPointer shorthand | | 29 ∣ 29 | 29 ∣ sp | stack pointer, Pointing to the top (Stack Pointer shorthand) | | 30 ∣ 30 | 30 ∣ s8 / fp ∣ (Save/FramePointer) Frame Pointer ∣ ∣ fp | (Save/Frame Pointer) Frame Pointer | | Fp ∣ (Save/FramePointer) frame pointer ∣ ∣ | $31 ra | general used to store the function return address (return address abbreviated) |

Register numbers and aliases correspond one by one. The same register can be represented in two different ways: 0∗∗ or ∗∗0** or **0∗∗ or ∗∗ Zero

  • Program Counter (PC) cannot be modified directly, but can be changed by jump instruction
  • HI and LO: These two registers are specifically used to hold the results of multiplication, division, and multiplication accumulation.

Segmentation processing in MIPS assembly

.data # text # code segmentCopy the code

Transfer instructions

  1. Load the immediate count instructionli

Li (load immediate) : transfers immediate data to a register

Li $t0, 1; Hexadecimal data is represented with a 0x prefixCopy the code
  1. Load address instructionla

Load Address (LA) : used to transfer an address to a register and obtain an address in a data segment from an address

.data MSG:.ascii "hello world". Text la $a0, MSG # assign the address of the string data to register $a0Copy the code
  1. Register data transfer instructionmove

Used to transfer data from one register to another

Move $t0,$t1 # move $t1 to $t0Copy the code

System service instructionsyscall

You can use printf to output text in C, but there is no such thing as printf in assembly. If you want to output text, you need to use the syscall instruction

If you want to output a digit 1, the **syscall instruction extracts the desired output from the $A0 register **

Therefore, you need to place the data in $a0 before executing syscall:

The syscall li $a0, 1Copy the code

You also need to specify the output data type, which is stored in the $v0 register

# $v0=1, syscall--->print_int
# $v0=4, syscall--->print_string
Copy the code

$v0 stores 1, indicating that Syscall will output the data in $a0 as a number

$v0 is stored to 4, indicating that Syscall treats the data in $A0 as the address of the data and prints the corresponding data

The syscall command reads and writes the table

Service Code in $v0 Arguments Result
print integer 1 $a0 = integer to print
print float 2 $f12 = float to print
print double 3 $f12 = double to print
print string 4 $a0 = address of null-terminated string to print
read integer 5 $v0 contains integer read
read float 6 $f0 contains float read
read double 7 $f0 contains double read
read string 8
a 0 = a d d r e s s o f i n p u t b u f f e r a0 = address of input buffer
a1 = maximum number of characters to read
See note below table
sbrk (allocate heap memory) 9 $a0 = number of bytes to allocate $v0 contains address of allocated memory
exit (terminate execution) 10
print character 11 $a0 = character to print See note below table
read character 12 $v0 contains character read
open file 13
a 0 = a d d r e s s o f n u l l t e r m i n a t e d s t r i n g c o n t a i n i n g f i l e n a m e a0 = address of null-terminated string containing filename
a1 = flags $a2 = mode
$v0 contains file descriptor (negative if error). See note below table
read from file 14
a 0 = f i l e d e s c r i p t o r a0 = file descriptor
a1 = address of input buffer $a2 = maximum number of characters to read
$v0 contains number of characters read (0 if end-of-file, negative if error). See note below table
write to file 15
a 0 = f i l e d e s c r i p t o r a0 = file descriptor
a1 = address of output buffer $a2 = number of characters to write
$v0 contains number of characters written (negative if error). See note below table
close file 16 $a0 = file descriptor
exit2 (terminate with value) 17 $a0 = termination result See note below table
Services 1 through 17 are compatible with the SPIM simulator, other than Open File (13) as described in the Notes below the table. Services 30 and higher are exclusive to MARS.
time (system time) 30
a 0 = l o w o r d e r 32 b i t s o f s y s t e m t i m e a0 = low order 32 bits of system time
a1 = high order 32 bits of system time. See note below table
MIDI out 31
a 0 = p i t c h ( 0 127 ) a0 = pitch (0-127)
a1 = duration in milliseconds
a 2 = i n s t r u m e n t ( 0 127 ) a2 = instrument (0-127)
a3 = volume (0-127)
Generate tone and return immediately. See note below table
sleep 32 $a0 = the length of time to sleep in milliseconds. Causes the MARS Java thread to sleep for (at least) the specified number of milliseconds. This timing will not be precise, as the Java implementation will add some overhead.
MIDI out synchronous 33
a 0 = p i t c h ( 0 127 ) a0 = pitch (0-127)
a1 = duration in milliseconds
a 2 = i n s t r u m e n t ( 0 127 ) a2 = instrument (0-127)
a3 = volume (0-127)
Generate tone and return upon tone completion. See note below table
print integer in hexadecimal 34 $a0 = integer to print Displayed value is 8 hexadecimal digits, left-padding with zeroes if necessary.
print integer in binary 35 $a0 = integer to print Displayed value is 32 bits, left-padding with zeroes if necessary.
print integer as unsigned 36 $a0 = integer to print Displayed as unsigned decimal value.
(not used) 37 to 39
set seed 40
a 0 = i . d . o f p s e u d o r a n d o m n u m b e r g e n e r a t o r ( a n y i n t ) . a0 = i.d. of pseudorandom number generator (any int).
a1 = seed for corresponding pseudorandom number generator.
No values are returned. Sets the seed of the corresponding underlying Java pseudorandom number generator (java.util.Random). See note below table
random int 41 $a0 = i.d. of pseudorandom number generator (any int). $a0 contains the next pseudorandom, uniformly distributed int value from this random number generator’s sequence. See note below table
random int range 42
a 0 = i . d . o f p s e u d o r a n d o m n u m b e r g e n e r a t o r ( a n y i n t ) . a0 = i.d. of pseudorandom number generator (any int).
a1 = upper bound of range of returned values.
$a0 contains pseudorandom, uniformly distributed int value in the range 0 = [int] [upper bound], drawn from this random number generator’s sequence. See note below table
random float 43 $a0 = i.d. of pseudorandom number generator (any int). $f0 contains the next pseudorandom, Uniformuniformdistributed float value in the range 0.0 = F 1.0 from this random number generator’s sequence.See note below table
random double 44 $a0 = i.d. of pseudorandom number generator (any int). $f0 contains the next pseudorandom, Uniformuniformdistributed double value in the range 0.0 = F 1.0 from this random number generator’s sequence.See note below table
(not used) 45-49
ConfirmDialog 50 $a0 = address of null-terminated string that is the message to user $a0 contains value of user-chosen option 0: Yes 1: No 2: Cancel
InputDialogInt 51 $a0 = address of null-terminated string that is the message to user
a 0 c o n t a i n s i n t r e a d a0 contains int read
a1 contains status value 0: OK status -1: input data cannot be correctly parsed -2: Cancel was chosen -3: OK was chosen but no data had been input into field
InputDialogFloat 52 $a0 = address of null-terminated string that is the message to user
f 0 c o n t a i n s f l o a t r e a d f0 contains float read
a1 contains status value 0: OK status -1: input data cannot be correctly parsed -2: Cancel was chosen -3: OK was chosen but no data had been input into field
InputDialogDouble 53 $a0 = address of null-terminated string that is the message to user
f 0 c o n t a i n s d o u b l e r e a d f0 contains double read
a1 contains status value 0: OK status -1: input data cannot be correctly parsed -2: Cancel was chosen -3: OK was chosen but no data had been input into field
InputDialogString 54
a 0 = a d d r e s s o f n u l l t e r m i n a t e d s t r i n g t h a t i s t h e m e s s a g e t o u s e r a0 = address of null-terminated string that is the message to user
a1 = address of input buffer $a2 = maximum number of characters to read
See Service 8 note below table $a1 contains status value 0: OK status. Buffer contains the input string. -2: Cancel was chosen. No change to buffer. -3: OK was chosen but no data had been input into field. No change to buffer. -4: length of the input string exceeded the specified maximum. Buffer contains the maximum allowable input string plus a terminating null.
MessageDialog 55
a 0 = a d d r e s s o f n u l l t e r m i n a t e d s t r i n g t h a t i s t h e m e s s a g e t o u s e r a0 = address of null-terminated string that is the message to user
a1 = the type of message to be displayed: 0: error message, indicated by Error icon 1: information message, indicated by Information icon 2: warning message, indicated by Warning icon 3: question message, indicated by Question icon other: plain message (no icon displayed)
N/A
MessageDialogInt 56
a 0 = a d d r e s s o f n u l l t e r m i n a t e d s t r i n g t h a t i s a n i n f o r m a t i o n t y p e m e s s a g e t o u s e r a0 = address of null-terminated string that is an information-type message to user
a1 = int value to display in string form after the first string
N/A
MessageDialogFloat 57
a 0 = a d d r e s s o f n u l l t e r m i n a t e d s t r i n g t h a t i s a n i n f o r m a t i o n t y p e m e s s a g e t o u s e r a0 = address of null-terminated string that is an information-type message to user
f12 = float value to display in string form after the first string
N/A
MessageDialogDouble 58
a 0 = a d d r e s s o f n u l l t e r m i n a t e d s t r i n g t h a t i s a n i n f o r m a t i o n t y p e m e s s a g e t o u s e r a0 = address of null-terminated string that is an information-type message to user
f12 = double value to display in string form after the first string
N/A
MessageDialogString 59
a 0 = a d d r e s s o f n u l l t e r m i n a t e d s t r i n g t h a t i s a n i n f o r m a t i o n t y p e m e s s a g e t o u s e r a0 = address of null-terminated string that is an information-type message to user
a1 = address of null-terminated string to display after the first string
N/A

Print the helloWorld example using the syscall command:

ASCII "hello world\0" # char* MSG ="hello world". Text la $a0, MSG li $v0,4 syscallCopy the code

Mips assembler instruction summary table

category

Instruction names

The instance

meaning

annotation

English annotation

calculate

The number

add

add $s1,  $s2,  $s3

$s1 = $s2 + $s3

Three register operands

Addition to add

subtraction

sub $s1,  $s2,  $s3

$s1 = $s2 – $s3

Three register operands

Subtraction subtraction

Immediate number addition

addi $s1, $s2,  20

$s1 = $s2 + 20

Used to add constant data

Add immediate add

The number

According to the

the

Lose,

Take the word

lw $s1, 20 ($s2)

$s1 = Memory[$s2 + 20]

To fetch a word from memory into a register

Load word Indicates the load word

characters

sw $s1, 20 ($s2)

Memory[$s2 + 20] = $s1

To fetch a word from a register into memory

Store word Stores words

Take half word

lh $s1, 20 ($s2)

$s1 = Memory[$s2 + 20]

To fetch half a word from memory into a register

Load halfword loads halfword

Take an unsigned halfword

lhu $s1, 20 ($s2)

$s1 = Memory[$s2 + 20]

To fetch half a word from memory into a register

load halfword unsigned

Save half word

sh $s1, 20 ($s2)

Memory[$s2 + 20] = $s1

To fetch half a word from a register into memory

Stroe Halfword stores half words

In bytes

lb $s1, 20 ($s2)

$s1 = Memory[$s2 + 20]

To fetch a byte from memory into a register

load byte

Take an unsigned byte

lbu $s1, 20 ($s2)

$s1 = Memory[$s2 + 20]

To fetch a byte from memory into a register

load byte unsigned

Remaining bytes

sb $s1, 20 ($s2)

Memory[$s2 + 20] = $s1

To fetch a byte from a register into memory

store byte

Take link words

ll $s1, 20 ($s2)

$s1 = Memory[$s2 + 20]

Take the word as the first half of the atomic exchange

load linked

Storage conditions of word

sc $s1, 20 ($s2)

Memory[$s2 + 20] = $s1;

$s1 = 0 or 1

Memory word as the second half of atomic exchange

store conditional

Take the high order of the immediate number

lui $s1, 20

$s1 = 20 * 216

Take the immediate number and place it in the highest 16 bits

load upper immediate

logic

album

with

and $s1, $s2, $s3

$s1 = $s2 & $s3

Three register operands by bit and

and

or

or $s1, $s2, $s3

$s1 = $s2 | $s3

Three register operands by bit or

or

Or not

nor $s1, $s2, $s3

$s1 = ~ ($s2 | $s3)

Three register operands by bit or not

not or

And immediately

andi $s1, $s2, 20

$s1 = $s2 & 20

And constant bitwise and

and immediate

The number immediately or

ori $s1, $s2, 20

$s1 = $s2 | 20

And constants by bit or

or immediate

The logical left

sll $s1, $s2, 10

$s1 = $s2 << 20

Shift to the left according to the constant

set left logical

Logic moves to the right

srl $s1, $s2, 10

$s1 = $s2 >> 20

Shift to the right by the constant

set right logical

article

a

points

the

Equal time jump

beq $s1, $s2, 25

if ($s1 == $s2) go to 

PC + 4 + 25 * 4

Equality detection:

PC – related jumps

branch on equal

Jump if not equal

bne $s1, $s2, 25

if ($s1 ! = $s2) go to

PC + 4 + 25 * 4

Unequal detection:

PC – related jumps

branch on not equal

Less than time jump

slt $1, $s2, $3

if ($s2 < $s3) $s1 = 1; 

else $s1 = 0

Is the comparison less than

set less than

The unsigned number is set to small

sltu $1, $s2, $3

if ($s2 < $s3) $s1 = 1; 

else $s1 = 0

Is less than an unsigned number

set less than unsigned

Set when an unsigned number is less than an immediate number

slti $1, $s2, 20

if ($s2 < 20) $s1 = 1; 

else $s1 = 0

Is the comparison less than a constant

set less than immediate

Set when unsigned number comparison is less than unsigned immediate number

sltiu $1, $s2, 20

if ($s2 < 20) $s1 = 1; 

else $s1 = 0

Is less than an unsigned constant

set less than immediate unsigned

There is no

article

a

jump

turn

jump

j 2500

go to 2500 * 4

Example Jump to the destination ADDRESS

jump

Jump to register position

jr $ra

go to $ra

For switch statements, as well as procedure calls

jump register

Jump and link

jal 2500

$ra = PC + 4;

go to 2500 * 4;

For procedure calls (methods)

Normal execution process after the execution of A naturally B instruction, now to jump to the execution of C method, at this time, B address stored in the register, after the execution of C jump to B

jump and link

Mips memory structure diagram: