.data
means the beginning of the data segment, .text
- the beginning of the code segment.v:
main:
loop:
and endloop:
.#
sign is comments . .data v: .word -1, -2, -3, -4, -5, -6, -7, -8, -9, -10 .text .globl main main: li $t0, 0 # $t0 = 0 (variable a) li $t1, 0 # $t1 = 0 (counter i) li $t2, 10 # $t2 = 10 (count limit l) loop: slt $t3, $t1, $t2 beq $t3, $zero, endloop la $t3, V sll $t4, $t1, 2 addu $t3, $t3, $t4 lw $t3, 0($t3) addu $t0, $t0, $t3 addiu $t1, $t1, 1 b loop endloop:
.data
section, in this case v:
and instruction labels (addresses of instructions in the .text
section, for example main:
loop:
..data
section is usually stored in memory starting at address 0x10010000. Instructions are stored starting at address 0x00400000. Since each MIPS assembler instruction occupies exactly 32 bits, the following label-address table will be true for our program:.data
, but more on that later..data
and .text
, and we already know that the first is for storing data and declaring global variables, and the second is for the program code itself. Look at the rest of the MIPS directives: .globl sym
declares the sym symbol global and allows you to access it from other files; .extern sym size
declares that the data that is stored in sym is of size size , and makes sym a global label (see previous directive); .ascii str
stores the string str in memory without adding a null character (\ 0) to the end; .asciiz str
saves the string str and appends a null character (\ 0) to the end; .byte b1, b2, ..., bn
successively stores bytes b1, b2, ..., bn ; .half h1, h2, ..., hn
consistently stores in memory 16-bit values h1, h2, ..., hn ; .word w1, w2, ..., wn
consistently stores in memory 32-bit values of w1, w2, ..., wn ; .dword dw1, dw2, ..., dwn
consistently stores in memory 64-bit values dw1, dw2, ..., dwn ; .float f1, f2, ..., fn
keeps in memory the floating-point numbers f1, f2, ..., fn ; .double d1, d2, ..., dn
remembers floating-point numbers (double precision) d1, d2, ..., dn ; .space n
allocate n bytes in this data segment; .align n
align all the following data to 2 ^ n bytes..align 1
in .data
. In this case, even if we store a value of 1 byte in the address 0x10010000, for example, the next byte will be left empty, and if we want to write another byte in memory, it will already receive the address 0x10010002. In MIPS, automatic alignment of data is enabled by default, and therefore you can write a 16-bit value ( .half
) only into an even memory address (0x10010000, 0x10010002, but not 0x10010003), a 32-bit value — only an address that is a multiple of 4, and 64 -bit - only to the address multiple of 8..data
.data
written in a fairly free manner. You just need to specify the label, data type and value. In this code there are several examples of correct data writing: .data var1: .byte 'A', 0xF3, 127, -1, '\n' var2: .half -10, 0xffff var3: .word 0x12345678 var4: .float 12.3, -0.1 var5: .double 1.5e-10 var6: .dword 0x1234567812345678 str1: .ascii “i love mips\n" str2: .asciiz “zero-finished string" array: .space 100
int
is entirely placed into it. To store a variable of type long
you must use two registers at once. Each register can be accessed by its ordinal name and by its common name. The overall is a bit more human-readable. The following registers are available: la rdest, addr
goes to the instruction set: lui $at, hi(addr) ori rdest, $at, lo(addr)
add $t2, $t0, $t1
In this case, the result of adding values in $ t0 and $ t1 will be placed in $ t2. addi $t3, $t2, 12
After execution, the result of adding $ t2 and the number 12 will be placed in the register $ t3. j 128
will go to address 128 in .text
.syscall
is one of the most simple, but at the same time one of the most significant instructions of the MIPS-assembler. This is a service instruction, so it is considered separately from the rest. syscall
used to access the operating system to perform actions that the processor itself is unable to perform. Before calling this instruction, you need to put the service code in the $ v0 register - a natural number from 1 to 12. Depending on the code, the operating system will perform one or the other action. Here is a list of service codes and their corresponding operating system actions that MARS supports:rd
is the register where the result is written, rs
is the first argument, rt
is the second argument. imm16
- a 16-bit integer or imm5
- a 5-bit natural number can also occur. add rd, rs, rt
the sum of rs and rt is written to the rd register. Gentle, may cause overflow. sub rd, rs, rt
rd = rs - rt. You can also get an overflow. addu rd, rs, rt
almost the same as the previous instruction, but this cannot cause overflow. For arithmetic calculations, it is preferable to use this particular instruction. subu rd, rs, rt
rd = rs - rt. Also without overflow, and therefore recommended to use. addi rd, rs, imm16
rt = rs + 16-bit integer. Like add
, it can cause overflow. addiu rd, rs, imm16
the same, but without the possibility of overflow. Use it. addiu $s1, $zero, 0xFFFF # $s1 = 0x0000FFFF ( )
addiu $s1, $zero, -0xFFFF # $s1 = 0xFFFF0001 ( 2)
int f = (g+h) - (ij);
addu $t0, $s1, $s2 # t0 = s1 + s2 = g + h subu $t1, $s3, $s4 # t1 = s3 - s4 = i - j subu $s0, $t0, $t1 # s0 = f = t0 - t1 = (g+h) - (ij)
java -jar Mars_4_2.jar
int a = b + c; int d = e + f; int g = a + b; int h = g + d;
Source: https://habr.com/ru/post/147685/
All Articles