پردازنده mips مقدمه دستورات کتاب patterson & henessi amirkabir...
Post on 21-Dec-2015
228 views
TRANSCRIPT
MIPS پردازنده
مقدمهدستورات
کتاب Patterson & Henessi
Amirkabir University of TechnologyComputer Engineering & Information Technology Department
مقدمهMIPS پردازندهای اولین از بصورت RISCیکی که است
. سال در است شده موفق و عرضه توسط 1984تجاری. است شده طراحی استانفورد دردانشگاه تیمی
. است قوی حال عین در ولی ساده ای پردازنده بصورت مختلفی تجهیزات شده embeddedدر استفاده
است: Various routers from Cisco Game machines like the Nintendo 64 and Sony
Playstation 2
ویژگیها
منظوره همه رجیسترهای زیاد تعداد دستورات کوچک مجموعه
MIPS32: 168 instructions MIPS64: 258 instructions
است متغیر آنها فرمت ولی ثابت دستورات اندازه دستورات به محدود حافظه به دسترسی
load/store است. است محدود دهی آدرس های مد
رجیسترها دارای پردازنده :32رجیستر 32این است بیتی
R0 .. R31 رجیسترR0 شده پر صفر مقدار با افزاری سخت بصورت
است صفر با برابر همیشه یعنی است رجیسترR1 است شده رزرو اسمبلر کار برای. نمود استفاده ها برنامه در میشود ها رجیستر بقیه از
داشته قرار ها رجیستر از یکی در باید همیشه ها عملوندباشند.
0r0r1°°°r31PClohi
فایل رجیستر
If Write = 1, then D data is stored into D address. You can read from two registers at once, by supplying
the A address and B address inputs. The outputs appear as A data and B data.
Registers are clocked, sequential devices. We can read from the register file at any time. Data is written only on the positive edge of the clock.
D data Write
D address
A address B address
A data B data
32 32 Register File
55
5
32
32 32
افزایش رجیسترها تعداد وقتیرجیستر بصورت آنها مییابد
: میشوند ساخته فایل
ها رجیستر سایر
فوق جیسترهای ر بر دارای MIPSعالوه: میباشد نیز دیگری رجیسترهای
PC (program counter) register and Status register
Floating point registers
برای رجیسترها نامگذاریافزار نرم در استفاده سهولت
0 zero constant 0
1 at reserved for assembler
2 v0 expression evaluation &
3 v1 function results
4 a0 arguments
5 a1
6 a2
7 a3
8 t0 temporary: caller saves
. . . (callee can clobber)
15 t7
16 s0 callee saves
. . . (callee must save)
23 s7
24 t8 temporary (cont’d)
25 t9
26 k0 reserved for OS kernel
27 k1
28 gp Pointer to global area
29 sp Stack pointer
30 fp frame pointer
31 ra Return Address (HW)
است شده داده اسمی رجیستر هر به باشد تر راحت اسمبلی نویسی برنامه اینکه برای
داده انواع
آنها اندازه اساس بر ها داده به رجوع مقایسه0% 20% 40% 60% 80%
Byte
Halfword
Word
Doubleword
0%
0%
31%
69%
7%
19%
74%
0%
Int Avg.
FP Avg.
شده حمایت های داده•Integer
•bit char-8•bit half-word-16•bit word-32•bit double-word-64
•Floating point• bit single-32precision• bit single-64precision• paired singleprecision•IEEE 754 standard
شده حمایت های داده•Integer
•8-bit char•16-bit half-word•32-bit word•64-bit double-word
•Floating point•32-bit single precision•64-bit single precision•paired single precision•IEEE 754 standard
MIPSحافظه
MIPS 32دارای . تا میتواند یعنی است آدرس حافظه 232خط محل . قرار داده بایت یک حافظه محل هر در نماید دهی آدرس را
میگیرند. This results in a 232 x 8 RAM, which would be 4 GB of
memory.
232 8 memory
ADRS OUTDATACSWR
8328
CS WR
Operation
0 x None1 0 Read selected address1 1 Write selected address
حافظه سازمان
دارای کلمه میباشد 4هر بایت
232 bytes with byte addresses from 0 to 232-1 230 words with byte addresses 0, 4, 8, ... 232-4
0
4
8
12
32 bits of data
32 bits of data
32 bits of data
32 bits of data
Registers hold 32 bits of data
حافظه : Alignmentسازمان
this word is aligned; the others are not!
addr
ess
0
4
812
16
20
24
31 071523
Words are aligned
حافظه : Alignmentسازمان . از میشود ایجاد بایت مبنای بر حافظه آدرس که باشید داشته توجه
کلمه یک .4بیتی 32اینرو نمود خواهد اشغال را حافظه محل
معماری بصورت MIPSدر باید . alignedکلمات گیرند قرار حافظه درکلمه یک آدرس 32یعنی که گیرد قرار حافظه از محلی یک در باید بیتیاز مضربی .4آن باشد
0, 4, 8 and 12 are valid word addresses. 1, 2, 3, 5, 6, 7, 9, 10 and 11 are not valid word addresses.
حافظه محل بک به دسترسی قصد اشتباه به که صورتی unalignedدرخطای یک باشد .bus errorداشته داد خواهد رخ
مالحظه قابل تاثیر باال سطح زبان با نویسی برنامه برای محدودیت این. کند عمل سریعتر اندکی تا میکند کمک پردازنده به اما ندارد ای
0 1 2 3 4 5 6 7 8 9 10 11
Word 1 Word 2 Word 3
Address
8-bit data
کلمات از ای آرایه
مثال برای ای آرایه اگر که بود مراقب ها آرایه با کار هنگام بایدمحل آدرس 2000از در آن اول عضو شود، شرع 2000حافظه
آدرس در آن دوم عضو آدرس 2004و در نه و بود 2001خواهد رجیستر اگر مثال مقدار a0$برای :2000دارای باشد
lw $t0, 0($a0)
حالیکه در میکند اشاره عضو اولین به
lw $t0,8($a0)
آدرس در که آرایه عضو سومین پیدا 2008به دسترسی است. نمود خواهد
در کلمه یک های بایت ترتیبحافظه
وجود حافظه در بایتها ترتیب کردن مشخص برای روش دودارد:
Big endian: word address (lowest numerical byte address) is the address of
the most significant byte Little endian:
word address is the address of the least significant byte
MSB LSBB+0 B+1 B+2 B+3Big Endian Byte
Little Endian ByteB+3 B+2 B+1 B+0
B is some base addressB is some base address
در کلمه یک های بایت ترتیبحافظه
Big Endian Little Endian
78
3412
5612
5678
34
200201202203
memory address
similar to writing English
B=200 is the base address in this exampleB=200 is the base address in this example
معماری . MIPSدر کرد تنظیم روش دو این از یک هر برای را پردازنده میتوان
دستورالعملهای اصلی انواعMIPS
Arithmetic Integer Floating Point
Memory access instructions Load & Store
Control flow Jump Conditional Branch Call & Return
محاسباتی دستورات
: دارند وجود محاسباتی دستورات دسته چهار Add Subtract Multiply Divide
محاسباتی دستورات دستورت ضرب ALUتمامی و جمع دستورات نظیر
: 3دارای دیگر تای دو و مقصد برای یکی هستند عملوند . از یکی باید ها عملوند سه هر ها داده مبدا برای
های . MIPSرجیستر محاسبات تمامی بیتی 32باشندهستند.
C code: A = B + C;E = F - A;
MIPS code: add $t0, $s1, $s2sub $s4, $s5, $s0
Unsigned arith: addu/subu (overflow undetected)Assembly language format: label: operation dest reg, first src reg, second src reg # Comment
محاسباتی دستورات
: معماری اصول رجیسترها های داده روی بر محاسبات تمامی
. حافظه در که را عددی نمیتوان یعنی میشود انجام . برای کرد جمع رجیستر یک با است شده ذخیره
منتقل به رجیستر به حافظه محتوی باید ابتدا اینکارانجام ها رجیستر های داده روی بر عملیات و شده
شود. : مقصد اول است ثابت همیشه اپراندها ترتیب
. میشود نوشته
برنامه در توضیحات نوشتناسمبلی زبان به MIPSنویسی
Hash (#) is used for MIPS comments anything from hash mark to end of line is a
comment and will be ignored
محاسباتی دستورات
Add/Sub_Immediate instructions
عدد رجیستر 16یک از یکی با را عالمت بدون یا و عالمت با بیتی.16های مینماید / تفریق جمع بیتی
Destination Reg = Source Register + Immediate Example: A = A - 4 addi $t0, $t0, -4 # $t0 = $t0 –4
Signed/Unsigned Arithm: addi, addiu
Assembly language format(I-format): label: operation dest_reg, src_reg, immediate value/constant # Comment
مثال
برنامه اسمبلی Cتبدیل MIPSبه
a = b + c + d - e; Break into multiple instructionsadd $t0, $s1, $s2 # temp = b + c
add $t0, $t0, $s3 # temp = temp + d
sub $s0, $t0, $s4 # a = temp - e Notice: A single line of C may break up into
several lines of MIPS.
مثال
How do we do this?
f = (g + h) - (i + j); Use intermediate temporary registeradd $t0,$s1,$s2 # temp = g + h
add $t1,$s3,$s4 # temp = i + j
sub $s0,$t0,$t1 # f=(g+h)-(i+j)
منطقی دستورات در موجود منطقی ازدستورات MIPSبرخی
AND bit-wise AND between registers and $t1, $s0, $s1
OR bit-wise OR between registers or $t1, $s0, $s1
NOR Bit-wise NOR between registers nor $t1, $s0, $s1 nor $t1, $t0, $0 # $t1 = NOT($t0)
Immediate modes andi and ori
حافظه به دسترسی دستورات میکنند منتقل رجیسترها و حافظه بین را ها اپراند 3دارای .داده
میباشند: LW/SW instruction:
میشود محاسبه زیر بصورت حافظه در داده آدرس
Source Address = Source Base Address + Offset
Assembly language format(I-format): label: operation dest_reg, offset ( src_reg) # Comment
Load/Store
Name of register to put value in
A number Name of register to get base address from
Load Wordمثال :
lw $s0, 4($s3)
If $s3 has the value 100, this will copy the word at memory location 104 to the register $s0.
$s0 <- Memory[104]
Store Wordمثال :
sw $s0, 4($s3)
If $s3 has the value 100, this will copy the word in register $s0 to memory location 104.
Memory[104] <- $s0
حافظه از خواندن
lw R6, 0(R5) # R6 <= mem[0x14]
مثال
C code: A[8] = h + A[8];assume h in $s2 and base address of the array A in $s3
MIPS code: lw $t0, 32($s3)add $t0, $s2, $t0sw $t0, 32($s3)
میکند کمک استراکچرها و ها آرایه سازی پیاده به و کرده ساده را ها دستورالعمل طراحی دهی آدرس نحوه این
حافظه به دسترسی دستورات
Mnemonic Instruction
LB Load Byte
LBU Load Byte Unsigned
LH Load Halfword
LHU Load Halfword Unsigned
LW Load Word
SB Store Byte
SW Store Word
SH Store Halfword
مثال C code: z = w + y; MIPS code: la $t0, w # put address of w into $t0 lw $s0, 0($t0) # put contents of w into $s0 la $t1, y # put address of y into $t1 lw $s1, 0($t1) # put contents of y into $s1 add $s2, $s0, $s1 # add w + y, put result in $s2 la $t2, z # put address of z into $t2 sw $s2, 0($t2) # put contents of $s2 into z
la= load address , lw= load word , sw= store word Must load address (la) to get the address of the memory location
into a register 0($t0)means go 0 bytes from the address specified by$t0
1010101010101010 0000000000000000
0000000000000000 1010101010101010
1010101010101010 1010101010101010
ori
1010101010101010 0000000000000000
filled with zeros
Then must get the lower order bits right, i.e.,ori $t0, $t0, 1010101010101010
مقادیر بالدرنگ بیتی 32انتقالرجیسترها به
انتقال با مرحله دو در آنها 16اینکار ترکیب و ارزش کم و ارزش با بیتمیپذیرد صورت
16 دستور با ارزش با .load upper immediateبیت میشود منتقل lui $t0, 1010101010101010
کنترلی دستورات
تغییر را برنامه اجرای سیر دستورات اینباید که بعدی دستور اینکه یعنی میدهند
میکنند تعیین را شود .اجراکنترلی دستورات مختلف انواع
conditional branches jumps (unconditional branch)procedure callsprocedure returns
انشعاب دستورات در شرطی انشعاب :MIPSدستورات از عبارتند
bne $t0, $t1, Label beq $t0, $t1, Label
Example: if (i==j) h = i + j;
bne $s0, $s1, Labeladd $s3, $s0, $s1
Label: .... Note the reversal of the condition from equality to
inequality!
کنترلی دستورات در غیرشرطی انشعاب :MIPSدستورات از عبارتند
j labelUnconditional jumpjr $t0 “jump register”. Jump to the instruction specified in
register $t0
Example:
if (i!=j) beq $s4, $s5, Lab1 h=i+j; add $s3, $s4, $s5else j Lab2 h=i-j; Lab1:sub $s3, $s4, $s5
Lab2: ...
کنترلی دستورات
استفاده نیز دستوردیگری از فوق دستور دو کنار در اغلبمیشود:
slt and slti // set if less than (w/ and w/o an immediate)
slt $t0, $s1, $s2
: میشود استفاده قبلی دستورات همراه به دستور این از
if $s1 < $s2 then $t0 = 1
else $t0 = 0
slti $at, $a0, 5 # $at = 1if $a0 < 5bne $at, $0, Label #Branch if $a0 < 5
آدرسدر محاسبه نحوهکنترلی دستورات
PC-relative addressing نوع از دستورات این آدرس I-Typeفرمت آن در که است
مقدار یک . 16بصورت مقدار این میشود نوشته بیتیرجیستر بصورت مقدار به نسبت افست نوشته PCیکمیشود.
op rs rt 16 bit addressI
bne $t4,$t5,Label Next instruction is at Label if $t4 $t5beq $t4,$t5,Label Next instruction is at Label if $t4 = $t5
آدرسدر محاسبه نحوهکنترلی دستورات
j Label Next instruction is at Label
Pseudodirect addressing ارزش با های بیت از فقط دستور این استفاده PCدر
میشود.32-bit jump address = 4 Most Significant bits of PC
concatenated with 26-bit word address (or 28- bit byte address)
Address boundaries of 256 MBFor larger distances: Jump register jr required.
op 26 bit addressJ
Example
LOOP: mult $9, $19, $10 # R9 = R19*R10 lw $8, 1000($9) # R8 = @(R9+1000)
bne $8, $21, EXIT add $19, $19, $20 #i = i + j
j LOOP EXIT: ......
Assume LOOP is placed at location 80000
op rs rt
0 19 10 9
35 9 8 1000
5 8 21 8
0 19 20
2 80000
...
80000
80004
80008
80012
80016
80020
19 0
0 24
32
سازی ازپیاده LOOPمثالی
MIPS: Assume: x , y , and sum are in $s0 $s1 , and $s2 respectively. Will use $t0 for I and $t1 for the
constant 1
C code: sum = 0; for (i = 0; i < y; i++) sum = sum + x;
add $s2, $zero, $zero # sum = 0add $t0, $zero, $zero # i = 0
LoopBegin:slt $t2,$t0, $s1Beq $t2, $zero, LoopEnd # is i < y ??add $s2, $s2, $s0 # sum = sum + xadd $t0, $t0, $t1 # i++j LoopBegin
LoopEnd:
ازآن بازگشت و توابع زدن صدا
استفاده فرعی برنامه زدن صدا برای دستور این ازمیشود:
jal: jump & link instruction
jal stores the return address in R31 and jumps to address in rt
رجیستر در را برگشت آدرس دستور میدهد R31این قرار. میکند انشعاب دردستور شده ذکر آدرس به و
استفاده زیر دستور از فرعی برنامه از بازگشت برایمیشود:
jr Reg[R31]
صدا هنگام در رجیسترها ذخیرهتودرتو توابع زدن
رجیسترهائی محتوی تا است الزم رجیسترها محدودیت بعلتذخیره تابع زدن صدا از قبل هستند مهم تابع برای که را
. نمود بازیابی را آنها تابع از بازگشت از بعد و نموده . ها برنامه اینکه برای شد خواهند ذخیره پشته در رجیسترها
زیر قاعده نمایند ذخیره را رجیسترها تمامی نباشند مجبور: میشود رعایت
: میکند بازیابی و ذخیره را زیر رجیسترهای زننده صدا تابع t0-$t9 $a0-$a3$v0-$v1
: میکند بازیابی و ذخیره را زیر رجیسترهای شونده صدا تابع$s0-$s7$ra
شیفت دستورت
MIPS Shift Instructions• Format: op code/dest reg/src reg/shift amount
sll $t4, $t0, 5 #shift left logical 5 bits (multiply by 32) sra $t5, $t0, 2 #shift right arithmetic 2 bits (divide by 4),
#sign extended srl $v0, $t0, 1 #shift right logical 1 bit. Sign bit is now 0 srlv $v0, $t0, $t1 #shift right logical, $t1 says how many bits
Type of shift Left Right
Logical sll sllv srl srlv
Arithmetic (none: use sll) sra srav
Rotate rol ror
دستورات خالصه MIPS
— loading words but addressing bytes— arithmetic on registers only
Instruction Meaning
add $s1, $s2, $s3 $s1 = $s2 + $s3sub $s1, $s2, $s3 $s1 = $s2 – $s3lw $s1, 100($s2) $s1 = Memory[$s2+100] sw $s1, 100($s2) Memory[$s2+100] = $s1
برنامه یک از Cمثالی
swap(int v[], int k);{ int temp;
temp = v[k]v[k] = v[k+1];v[k+1] = temp;
}
swap:muli $2 , $5, 4add $2 , $4, $2lw $15, 0($2)lw $16, 4($2)sw $16, 0($2)sw $15, 4($2)jr $31
Explanation: index k : $5 base address of v: $4 address of v[k] is $4 + 4.$5
دستورات فرمت
و ثابت مانو پایه کامپیوتر دستورات طول و فرمت: . کلی حالت در بود یکسان
با دستوراتی از باشد مهم شده تولید کد اندازه اگرمیشود استفاده متفاوت فرمت
( با ( دستوراتی از باشد مهم سرعت کارائی اگر اما. میشود استفاده یکسان طول
Variable:
Fixed:
……
دستورات MIPSفرمت دستور هر .32طول است بیت تعدادی به دستور . Fieldهر فیلد هر میشود تقسیم
. میدهد ارائه العمل دستور مورد در توضیحی توضیحات ارائه نیازمند مختلف دستورات آنجائیکه از
در لذا هستند ولی ( MIPSمختلفی مختلف فرمت نوع سه. ( است شده گرفته نظر در دستورات برای یکسان طول با R-format Register instructions are used for register based ALU
operations. I-format Immediate instructions, can be either Load/Store
operations, Branch operations, or Immediate ALU operations. J-format Jump instructions, devote all of the non-opcode space to a
26-bit jump destination field.
دستورات MIPSفرمت
3: هستند زیر بصورت مختلف فرمت نوع
opcode Offset added to PC
6 26
opcode rs rd immediate
6 5 5 16
I-Type
R-Type
J-Type
opcode rs rt
6 5 5
rd
5
shamt
5
func
6
Note the regularity of instruction encoding. This is important for implementing an efficient pipelined CPU.
دستورالعمل مختلف های فیلدها
op operation of the instruction rs first register source operand rt second register source operand rd register destination operand shamt shift amount funct function (select type of ALU operation)
add = 32 sub = 34
مثال
Example: add $t0, $s1, $s2 registers have numbers, $t0=8, $s1=17, $s2=18
Instruction Format:
000000 10001 10010 01000 00000 100000
op rs rt rd shamt funct
R-Typeدستورات
opcode rs rt
6 5 5
rd
5
shamt
5
funct
6
opcode: basic operation to be performed
rs: source register 1 rt: source register 2 rd: destination register shamt: shift amount funct: specific variant of
opcode
دارای دستورات اینopcode=0 برای و بوده
استفاده ALUعملیات میشوند.
فیلد توسط نظر مورد عمل: میشود مشخص
add: 32
sub: 34
دارای دستورات اینopcode=0 برای و بوده
استفاده ALUعملیات میشوند.
فیلد توسط نظر مورد عمل: میشود مشخص
add: 32
sub: 34
دستور ADDفرمت
rsop rt rd shamt funct
6 bits5 bits6 bits 5 bits 5 bits 5 bits
32 bits
از غیر دستور زیادی تعداد برای فرمت نیز Addاین . دستورات نوع این همه که میشود نامیده R-typeاستفاده
میشوند.
دستور اندکنیگ For add:
op is 010 (000000) funct is 3210 (100000)
Register encodings: $s0 is 1610 (10000), $s1 is 1710, … $t0 is 810 (01000), $t1 is 910, …
:HEXبصورت با است برابر دستور این 02288020مقدار
rsop rt rd shamt funct
000000 10001 01000 10000 00000 100000
add $s0, $s1, $t0
Opcode lw: 35 (100011) sw: 43 (101011) opcode value differentiates I- and R-Type instructions
rs: base register rt:
destination register for lw source register for store
immediate: offset from base range: -215 to (215-1)
Similarity in opcode for lw & sw simplifies hardware
opcode rs rt immediate
6 5 5 16
I-Type Format
I-Typeدستورات
rsop rt address
16 bits6 bits 5 bits 5 bits
32 bits
rs is the base register
rt is the destination of a load (source of a store)
address is a signed integer
دستور SW LW/فرمت
lw $s0, 24($t1)
100011 01001 10000 0000000000011000
rsop rt address
sw $s0, 24($t1)
101011 01001 10000 0000000000011000
rsop rt address
آدرسدهی با دستورات فرمتبالدرنگ
دستورات . ALUبرای میشود استفاده دارند ثابت اپراند یک به نیاز که)e.g., X=X+4(
برای .loadهمچنین میرود بکار حافظه از ثابت مقادیر کردن عدد یک بصورت ثابت . 16مقادیر رنج در لذا میشوند کد to 215-بیتی
(215-1). بود خواهند:مثال
addi R4, R8, 79slti R1, R2, 56: sets Reg[R1]=1 if Reg[R2]<56 else Reg[R1]=0
opcode rs rt immediate
6 5 5 16
I-Type Format
J-Typeدستورات
فرم به غیرشرطی پرش کد J-Typeدستوراتمیشوند.
دستور با Jاپکد دستور 2برابر اپکد با Jalو برابر3. باشد می
به نسبت جابجائی .PC+4مقدار میشود محاسبه
opcode Offset added to PC+4
6 26
J-Type Format
آدرسدهی های Mipsمد Immediate
add R4, #7 # Regs[R4]=Regs[R4]+7 16-bit field for the constant
Register add R4, R3, R2 # Regs[R4]=Regs[R3]+Regs[R2]
Displacement lw R4, 100(R1) #regs[R4]=Regs[R4]+Mem[Regs[R1]+100] 16-bits for displacement
Special cases of displacement mode indirect mode: displacement value=0
lw R4, 0(R1) #regs[R4]=Regs[R4]+Mem[Regs[R1]] absolute addressing : R0 as base register (always stores 0)
lw R4, 8769(R0)
آدرسدهی های Mipsمد
Byte Halfword Word
Registers
Memory
Memory
Word
Memory
Word
Register
Register
1. Immediate addressing
2. Register addressing
3. Base addressing
4. PC-relative addressing
5. Pseudodirect addressing
op rs rt
op rs rt
op rs rt
op
op
rs rt
Address
Address
Address
rd . . . funct
Immediate
PC
PC
+
+
Pseudo-instructions MIPS assemblers support pseudo-instructions that give the
illusion of a more expressive instruction set, but are actually translated into one or more simpler, “real” instructions.
In addition to the la (load address) we saw on last lecture, you can use the li and move pseudo-instructions:
li $a0, 2000 # Load immediate 2000 into $a0
move $a1, $t0 # Copy $t0 into $a1
They are probably clearer than their corresponding MIPS instructions:
add $a0, $0, 2000 # Initialize $a0 to 2000add $a1, $t0, $0 # Copy $t0 into $a1
یا Stackپشته
ذخیره جهت که است اصلی حافظه از محلی پشته. میشود استفاده ها داده
پشته از استفاده موارد برنامه یک متغیرهای ذخیره زده صدا فرعی برنامه یک که وقتی رجیسترها سایر محتوی ذخیره
میشود روش با محاسباتی عملیات ترجمه به RPNکمک
رجیسترSp را پشته در شده ذخیره داده آخرین محلمیکند مشخص
: میشوند تعریف پشته روی زیر عملیاتPush
SPSP-1M[SP]DR
POPDRM[SP] SPSP+1
برگشت های برنامه و پشتهپذیر
پیاده امکان تا میشود باعث پشته از استفاده. گردد فراهم پذیر برگشت های برنامه سازی
تابع از نسخه هر برای میتوان پشته از استفاده باگرفت نظر در ای جداگانه حافظه شده زده صدا
پشته در میتوان را محلی متغیرهای و آرگومانها. نمود ذخیره
به نسبت آرگومانها و محلی متغیرهای دهی آدرس. میشود انجام پشته موقعیت
که است حالتی عکس توابع از زده صبازگشت دااند شده
یا Stackپشته
درMIPS آدرسهای سمت به باال آدرسهای از پشته. میکند رشد پائین
هنگامpush مقدار باید پشته در داده کردن.sp$رجیستر داد کاهش را
از صدا stackاستفاده برایفرعی تابع زدن
پشته در رجیسترها مقادیر تابع زدن صدا از قبلمیشود ذخیره
: میشود زده صدا ازدستور استفاده با jalتابعaddress
اصلی برنامه به بازگشت برای تابع، اجرای پایان دردستور .jr (jump register)از میشود استفاده
پشته از رجیسترها مقادیر تابع از بازگشت از بعد. میشوند خارج
$sp
low address
high address
fille
de
mp
tySave $s0 and $s1:
subi $sp,$sp,8
sw $s0,4($sp)
sw $s1,0($sp)
Restore $s0 and $s1:
lw $s0,4($sp)
lw $s1,0($sp)
addi $sp,$sp,8
پشته از استفاده از مثالی
تابع سازی ازپیاده مثالیint func(int g, int h, int i, int j)
{ int f;f = ( g + h ) – ( i + j ) ;return ( f );
} // g,h,i,j - $a0,$a1,$a2,$a3, f in $s0func :
addi $sp, $sp, -12 #make room in stack for 3 words
sw $t1, 8($sp) #save the regs we want to use
sw $t0, 4($sp)sw $s0, 0($sp)add $t0, $a0, $a1 #$t0 = g + hadd $t1, $a2, $a3 #$t1 = i + jsub $s0, $t0, $t1 #$s0 has the result
add $v0, $s0, $zero #return reg $v0 has flw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1addi $sp, $sp, 12 # restore sp
jr $ra
we did not have to restore $t0-$t9 (caller save) we do need to restore $s0-$s7 (must be preserved by callee)
معکوس نمایشلهستانیReverse Polish Notation (RPN)
: ریاضی عبارات نمایش معمولی روشA*B+C*D
روشPN : عملگرها ریاضی عبارات نمایش برای. قرارمیگیرند عملوندها از قبل
*+AB*CD روشRPN : عملگرها ریاضی عبارات نمایش برای
میگیرند قرا عملوندها از بعدAB*CD+*
پیاده برای پشته از استفاده RPNسازی
پشته ترکیب از کامپیوترها و حساب ماشینهای برخی دراستفاده RPNو ریاضی عبارات محاسبه برای
میکنند. بصورت عبارت معموال ) RPNابتدا میشود نوشته
) میشود انجام کامپایلر توسط اینکار محاسبه هنگام در
پشته در را آنها عملوندها به برخورد میکنیم PUSHبا پشته باالی در موجود داده دو ، عملگرها با برخورد با
POP حاصل و انجام آنها روی بر نظر مورد عمل و شده.PUSHدرپشته میشود
مثال
است ممکن کامپایلر یک زیر عبارت محاسبه برای. نماید تولید را زیر ;a= b + c * dکد
خواهد زیر بصورت پشته محتوی اینصورت دربود:
PUSH bPUSH cPUSH dMULADDPOP a
b
PUSH b
مع :RPNدل اعبارت بود خواهد زیر بصورتbcd+*
b
c
PUSH c
b
c
d
b
c*d
PUSH d MUL
b+c*d
ADD pop
تمرین:
کمکی پردازنده مطالعه مورد MIPSبا در گزارشیدستورات و اعشاری floating pointمحاسبات
. دهید ارائه پردازنده دراین استفاده مورد