You are on page 1of 1

-301

MIPS Assembly
( )

:
:

/ (I/O - Input/Output): "" , -, , , , , , ( ), (network interfaces), .


(Main Memory): -- flip-flops --
( ) "" (words) (.. 32, 64) bits .
(array) bytes .
(Processor) (CPU - Central Processing Unit): (control)
. " " (datapath)
. , "
" (general-purpose registers). , (.. 32 ).
, ' ("register file") (
bits , ).

(, ),
"" (instructions) -- . [
, ( .. 32 64 bits ),
. (compiler)
].
. .
, -- "". , ..
, , , " ". ,
, "" , ' ' . ""
, " "....
"" , , "
" (PC - program counter) -- " " (program pointer),
"PC". , PC .
(array), M[ ], " " (index), i,
M[ i ]. C, pointer .

:
(HLL --High Level
Languages). hardware , " " (Machine
Language). "binary compatible" ,
. ("") , '
" " (RISC --Reduced Instruction Set Computers).
-- MIPS. RISC SPARC,
PowerPC, Alpha. x86 Pentium Intel .
(opcode --operation code), (operands)
. MIPS (registers) , ,
. CPU MIPS 32 32 bits --32 bits (word) MIPS. ,
, 64 bits. MIPS (3) , .
hardware,

Assembly MIPS:
, opcode,
, . "Assembly",
-- "Assembler". (HLL)
( C) Assembly , Compiler.

MIPS add . Assembly "add $23, $16, $18"


' 16 18, , ' 23: $23 := $16 + $18.
sub (subtract) , , : "sub $23, $16, $18"
' 23 $16 - $18.
addi (add immediate) add (), : "addi $23,
$16, 157" 16, 157 , 23.
i 18, "i=i+1" "addi $18, $18, 1".
$0 MIPS , (
hardware). , "i=1" : "addi $18, $0, 1".

$0, $1, ..., $31 32 MIPS, $s0, $s1, ..., $t0, $t1, ...
compiler, .
, "" , () .
, PC , ( PC)
( " ") . (CTI - control transfer instruction).
, , , , (branch) (jump). "j label" ("jump" to label, "goto" label)
"label", " " . , "j label"
"label" PC.

MIPS
, , "" ,
() . , PC ,
( PC) ( " ") . (CTI control transfer instruction). , j label ("jump" to label),
label, " " . , j label label PC.
1 " ",
"" .
( ) (conditional branch),
, . beq (branch if equal):
"beq $16, $17, label" 16 17, . (equal) label,
. , ,
" " . bne (branch if not equal) , (not equal),
" ".

: load store
MIPS, RISC, --
(immediate constants).
(32 bits), (16 bits), byte (8 bits) ' CPU,
, . () hardware, ()
, .
32- ' (" ") "lw $rd, imm($rx)" (load word),
$rd (destination register), $rx (index register)
imm ' $rd. ,
(array) M[ ], M[A] A. , lw
$rd, imm($rx) (imm + $rx), M[imm + $rx], $rd.
, 32- (" ") "sw $rs, imm($rx)" (store
word), (imm + $rx), M[imm + $rx] <-- $rs. , $rs
(source register) , (source operand)
Assembly, load.

Bytes :
MIPS, , Bytes , MIPS "Byte
Addressable". , 32- (.. ) 4 " " (4 bytes). , (array) 100
"" 400 () () . ' , 4. A0
"" () , a[0], C, a[ i ]
(A0 + 4*i). (char), a[ i ] (A0 + i).
4 bytes . 4 bytes
("") 4 . MIPS (alignment restrictions)
load store : N bytes N. , N
2, ' , bytes
(least significant) bits. , N bytes,
N bytes.

Bytes: Big-Endian Little-Endian


bytes (.. ),
() bytes . ,
, -- "big-endian" "little-endian".
bytes: ' ,
bits 2 " " (MS most significant) bits, 2 " " (LS - least significant) bits. byte
MS bits MS byte, LS bits LS byte. , ,
MS bits byte , LS bits byte , (, --
" transistors" " transistors"...).

, (4) (16 bytes) 32- "big-endian"


"little-endian". 2003 () = 7D3 (), 3
(array of char) 10 , bytes (null-terminated) string "katevenis"
( byte ASCII --.. byte 01101011, 'k'-- , ,
).

Big-Endian: , MS byte , bytes ()


"", LS byte . "big-endian" bytes "big end", MS
byte.
Little-Endian: , LS byte , bytes ()
"", MS byte . "little-endian" bytes "little end",
LS byte.

(.. 2003 12) , , ,


4 bytes ("") 4 . string
bytes , . ( C)
"char buf[10];", buf[] (.. compiler) 16 25 , i ,
buf[i], 16+i, 16 ( , buf[0]),
1 (byte). , 'k' buf[0] 16, 'a' buf[1]
17, ...
"endian-ness" , big-endian little-endian, ,
-- -- string
string, integer integer. "endian-ness" --
-- .. string integer, integer string.
endian-ness ' . ,
(.. ftp) (ASCII strings), bytes .
(.. 32- ) endian-ness, : k, a, t, e e,
t, a, k big-endian little-endian, 2003 00, 00, 07, D3 (), D3,
07, 00, 00, 11010011.00000111.00000000.00000000 (), -754,515,968 (, 2).
, (.. "type" ftp).

MIPS:
, . , Assembly
" ", . . , Assembler.
MIPS, RISC, 32 bits. 2 6 (fields). 3
. (format) , op,
6 bits, (MS bits) . , 6 bits , , format ,
opcode , . :
R-format:
( ), op, 5 bits 32 ,
5 bits , 6 bits
op funct (function code).
I-format:
, op, 5 bits 32 , 16 bits
imm (immediate, offset, constant, address) "" .
J-format:
, op, "target" 26 bits .
MIPS, format , opcodes A-53 A-75 . format
opcode op funct 5.

, , MIPS
(CTI, control transfer instructions) -- --
" " . , (branch).
, (jump). , ,
.
MIPS, , .
( " " MIPS),
(, , ). :

beq $rs, $rt, label


bne $rs, $rt, label

# : $rs == $rt
# : $rs != $rt

Assembly, (label), Assembler


. , I-format, :
PC_new := (PC_br + 4) + 4 * ImmOffset
PC_br , ImmOffset 16 bits I-format
2 ( sign-extended), PC_new .
(PC_br+4) hardware ( PC 4). ImmOffset 4
MIPS 4, ""
-- , ImmOffset () (), bytes .
(: , MIPS " " (delayed branches), (pipelining)
hardware, , , -- .
, , , , .
, Boolean . Boolean
: 0 , 1 . , (32) ,
0 1, LS bit , bits .
MIPS :

slt $rd, $rs, $rt # set less than -- : $rs < $rt. , Boolean,
$rd.
slti $rd, $rs, imm # set less than immediate -- (sign-extended) imm:
$rs < imm. , Boolean, $rd.

, MIPS
:
j target
(jump) : target. J-format,
(32 bits) 4 MS bits PC, 26 bits , 2 LS bits,
jr $rs
(jump register):
rs ( , $rs , pointer ).
( ) ,
(run-time variable) (data dependent).
.
jal target
(jump and link) -- : format jump, ( ),
, , ( PC 4) 31 ($ra, $31).
. , jr $ra.
$ra ($31) -- "31" jal, hardware .

MIPS
(procedure call) jal (jump and link - ), (procedure return)
jr (jump register - ), 6.

:
,
-- , "" () ( )
. ( ), compilers
MIPS . ( (separate compilation)
). ( (instance)
). :

: " " (caller),


: " " (callee).

$t0 - $t9 ($8-$15 $24-$25) -- temporary registers:


(not preserved across call), ( )
,
. ,
, ,
("caller-saved"), (. ).
(lifetime) ,
( ) .
[: " (lifetime)" ( )
,
. ,
/, "/", / --
-- ].
$s0 - $s7 ($16-$23) -- saved registers: (preserved across
call), ("callee-saved"),
. , ,
, , -- -- ' .
(lifetime) , ,
, , ( )
, (
).

$ra ($31) Return Address: ,


(. )
( ), , .
$sp ($29) Stack Pointer, $fp ($30) Frame Pointer: . compilers frame pointer --
. 3.6, . frame pointer ()
. .6 .
$gp ($28) Global Area Pointer: ( ) . 64 KBytes
load store ,
(global) (scalar) .
$k0-$k1 ($26-$27) Kernel Reserved, $at ($1) Assembler Temporary: compilers --
Assembler.
$zero ($0) = 0: (hardwired) . , (hardwired) .
$a0-$a3 ($4-$7) Procedure Argument Registers: 4 (arguments) . 4 ,
. 4 , ( ) $a3-$a0
$t0-$t9. .., , $a2, $a3
, :
,
$a2, $a3. ' , $a0, $a1 --
, (
).
$v0-$v1 ($2-$3) Procedure Return Values: ( $v0) ( $v1
). .
(.. $v1) $t0-$t9.

:
, -- ,
,
. , ( , re-entrant procedures),
. "last in first out" (LIFO),
(stack).
() MIPS 7f.ff.ff.ff (), ,
( 0). $sp .
$sp 4, ( , (interrupt))
. ( $sp 4 -- )
. load store
$sp. $sp / ,
$sp , compiler/ ( --
-- $fp).

(Global) (Local) :
"" (global) "" (local) (HLL - .. C, ). Assembler
, Assembly ( compiler). HLL,
, ( ),
, . ,
, ()
, . ,
( () !) !
, (compilers) --
. , ,
,
.
( ) ( ).
, .
, , stack-pointer,
, -- --
. (, , "", ),
( )
, .
" " (global labels) Assembler ".globl".
Assembler -- , , .
Assembler , , "" Assembly
, --
Assembly.

You might also like