Read H&P section 3.5 and 3.6 MIPS assembler language de branching Kosula bagli olarak farkli instruction lar run et High-level language lerde IF statement ine benzer Similar to IF statement in a high-level language Assembly language branch veya jump instruction lari kullanarak programin farkli bolumlerine program executione yonlendirir 12/3/2004 Bilgisayar Mimarisi 9.1 12/3/2004 Bilgisayar Mimarisi 9.2 Branching in MIPS Branch la erisilebilen herhangi bir staement e label atanir workhere: add $t0, $t1, $t2 Branch Conditional (data-dependent) Unconditional (daima dallan) Branching in MIPS Conditional branche lar cok siklikla kullanililir High-level languages If statetements, loops (for, while, do-while) MIPS I formatta beq ve bneq instructionlarina sahip beq $t0, $t1, label # go to label if t0 = t1 bneq $t0, $t1, label # go to label if t0!= t1 12/3/2004 Bilgisayar Mimarisi 9.3 12/3/2004 Bilgisayar Mimarisi 9.4
Example of MIPS equality test if (a == b) c = c + 9; c = c + 5; // Java code here, MIPS // assy code below lw $t0, a # a, b, and c into registers lw $t1, b lw $t2, c bne $t0, $t1, add5 # bypass next line if a=b addi $t2, 9 # c = c + 9 add5: addi $t2, 5 # c = c + 5 sw $t2, c # result back to memory BNE not enough for if-else Asagidaki Java yapisi A nin hesaplanmasindan sonra unconditional jump instruction ina ihtiyac duyar if (a == b) do calculation A else do calculation B Eger a = b, B yi hesaplayan code uzerinden dallanilmali 12/3/2004 Bilgisayar Mimarisi 9.5 12/3/2004 Bilgisayar Mimarisi 9.6 Fabricate an unconditional branch? Unconditional Jump beq $t0, $t0, label # Daima dallan j label # always jump Unconditional branch dan yavas olacaktir MIPS gercek unconditional branch a sahip hizli J format: op address 6 26 Note: The address specifies words, not bytes, thus extending its range by 4 times 12/3/2004 Bilgisayar Mimarisi 9.7 12/3/2004 Bilgisayar Mimarisi 9.8
Example of MIPS if-else construct if (a == b) c = c + 9; else c = c + 5; // Java code here, MIPS // assy code below lw $t0, a # a, b, and c into registers lw $t1, b lw $t2, c bne $t0, $t1, add5 # bypass next line if a=b addi $t2, 9 # c = c + 9 j more # continue ahead add5: addi $t2, 5 # c = c + 5 more: sw $t2, c # result back to memory 12/3/2004 Bilgisayar Mimarisi 9.9 Address widths in 3 instruction formats R Format I Format J Format 6 5 5 5 5 6 OP RS RT RD shamt funct OP RS RT address OP address 16 bits 26 bits BEQ uses I format, branch address has 16 bits, so branch can only cover 64K bytes, which is 16K words. Thus a program can only fit in 64K bytes -- too small. J uses J format, branch address has 26 bits, so branch can cover 64M words. 12/3/2004 Bilgisayar Mimarisi 9.10 Extending the BEQ branch range 64 Kbyte program cok kucuk. Daha sonra BEQ instruction in range ini artirma yontemlerini gorecegiz. Branch gercekte ne demek PC ye fetch edilecek yeni instruction in address ini yukle Normal non-branching instruction larda sonraki instruction i execute et prensibi PC nindegeri 4 artirilarak sonraki instruction in address i bulunur 12/3/2004 Bilgisayar Mimarisi 9.11 12/3/2004 Bilgisayar Mimarisi 9.12
Branch on signed data Genel instructions blt,ble,bgt, bqe. Ornek: blt A,B,label # eger A <= B ise dallan MIPS hardware bu tip instructionlara sahip degil Assembler blt instruction i slt ve bne instruction larina cevirir (bne slt yi izler) blt instruction pseudo instruction olarak adlandirilir Set on Less Than (slt) slt $t0, $t1, $t2 t0 is set to 1 if t1 < t2, unchanged otherwise R-format (doesn t need a memory address) 12/3/2004 Bilgisayar Mimarisi 9.13 12/3/2004 Bilgisayar Mimarisi 9.14 How to do a blt blt $t1, $t2, overthere # does the same thing pseudoinstruction -- assembles into slt + bne slt $t0, $t1, $t2 # t0 = 1 if t1 < t2 bne $t0, $zero, overthere # branch if t1 < t2 for loop nasil gerceklenir Sonraki slide larda uc ornek Diger sekiller de mevcut (Note: $zero is a register that always = 0) 12/3/2004 Bilgisayar Mimarisi 9.15 12/3/2004 Bilgisayar Mimarisi 9.16
for (i=5; i>0; i++) { } for(i=0; i<5; i++) { } ntm:.word 5 # number of times to loop ntm:.word 5 # number of times to loop lw $t1, ntm # Start index at 5 loop: do stuff do stuff addi $t1,-1 # decrement index bne $t1,$zero,loop # loop til index is 0 lw $t0, ntm # Max loop value to t0 add $t1,$zero,$zero # clear index loop: do stuff do stuff addi $t1,1 # increment index slt $t2, $t1, $t0 # t2=1 if index < max bne $t2,$zero,loop # loop while index < max 12/3/2004 Bilgisayar Mimarisi 9.17 12/3/2004 Bilgisayar Mimarisi 9.18 for (i=0; i<5; i++) { } use BLT pseudoinstruction ntm:.word 5 # number of times to loop lw $t0, ntm # Max loop value to t0 add $t1,$zero,$zero # clear index loop: do stuff do stuff addi $t1,1 # increment index blt $t1, $t0, loop # loop while index < max 12/3/2004 Bilgisayar Mimarisi 9.19 Non-MIPS loop-control instructions Cok yaygin olduklarindan bazi hardware ler tarafindan gerceklenir Iki ornek sob R0, loop Substract one and branch PDP-11 family bc loop Branch conditional loop Power Pc ozel bir loop register a sahip 12/3/2004 Bilgisayar Mimarisi 9.20
MIPS branching instructions H&P pages A-61 A-65 Pseudoinstructions Assembler language instructions, fakat hardware tarafindan taninmazlar (hardware direkt execute edemez) Assembler hardware in direkt execute edebilecegi bir veya bir kac instruction a cevirir Code yazimini kolaylastirir Kodlamayi daha genel hale getirir Hardware i expand etmeksizin instruction set expand edilir 12/3/2004 Bilgisayar Mimarisi 9.21 12/3/2004 Bilgisayar Mimarisi 9.22 Pseudoinstruction Translates into move $t0, $t1 add $t0, $t1,$zero blt $t0, $t1, label slt $t2, $t0, $t1 bne $t2, $zero, label li $t0, 23 addi $t0, $zero, 23 Pseudoinstruction lar gercek machinelevel instruction lar degil Kismi high-level language Hardware i degerlendirmede kullanilamazlar SPIM simulator hangi instruction lara donusturulduklerini gosterir 12/3/2004 Bilgisayar Mimarisi 9.23 12/3/2004 Bilgisayar Mimarisi 9.24
Registers - reality and conventions Butun register lar fiziksel olarak ayni Sadece $zero daima 0 icerir ve degistirilemez Kullanim sekli: Belli registerlar belli seyler icin kullanilir ve isimleri yaptiklari islere gore verilir 12/3/2004 Bilgisayar Mimarisi 9.25 MIPS register kullanim kurallari $zero 0 constant 0 $at 1 reserved for assembler $v0-$v1 2-3 results from functions $a0-$a3 4-7 saved registers, arguments $t0-$t7 8-15 temporary registers $s0-$s7 16-23 saved registers $t8-$t9 24-25 more temporary registers $k0-$k1 26-27 reserved for op sys $gp 28 global pointer $sp 29 stack pointer $fp 30 frame pointer $ra 31 return address 12/3/2004 Bilgisayar Mimarisi 9.26 Neyi bilmemiz gerekir Subroutine call nasil olur Subroutine nasil jump edilir How to jump to the subroutine Subroutine argument ler nasil gecirilir Subroutine den sonuclar nasil geri dondurulur Subroutine den geri nasil donulur Register lar nasil save edilir stack nasil calisir Last-In-First-Out storage (LIFO) Empty: Stack Pointer (SP) belli bir mem location a point eder Push: SP yi bir azalt, data yi SP nin point ettigi memory location ina yaz Pop: Data yi SP nin point ettigi bellek gozunden oku ve SP yi bir artir Bu is icin MIPS $sp adinda ozel bir register a sahip 12/3/2004 Bilgisayar Mimarisi 9.27 12/3/2004 Bilgisayar Mimarisi 9.28
100 104 Empty 108 SP -- > 112 116 Push 19 100 SP -- > 104 108 112 116 Operation of a stack 19 17 Push 17 100 104 SP -- > 108 112 116 Pop 100 104 SP -- > 108 112 116 17 17 12/3/2004 Bilgisayar Mimarisi 9.29 subroutine calls Arguments in $a0-3 Results in $v0-1 leaf fonksiyonlarda (diger fonksiyonlara cagri yapmayan) $t0-$t7 yi save-restore etmeye gerek yok Diger register lar fonksiyon cagriminda save edilir, fonksiyondan donusten hemen once ise restore edilir 12/3/2004 Bilgisayar Mimarisi 9.30 subroutine calls non-leaf function (diger fonksiyonlara cagri yapan) $t0-$t7 yi cagrilan fonksiyon icin save edilmeye gerek olmayan temporary variable lar icin kullan $s0-$s7 yi cagrilan fonksiyonda kullanilan degerler icin kullan $s0-$s7 cagrilan fonksiyonda degistirilecekse bunlari save et $sp tarafindan point edilen memory alanina register lari save et, local variable lari allocate et MIPS subroutine calls Jump and Link instruction (jal) Ornek: jal MySub Bir sonraki instruction in address i (PC+4) return address, return address ($ra) registerine save sedilir J formatinda, 26 bit lik address alanina sahip 12/3/2004 Bilgisayar Mimarisi 9.31 12/3/2004 Bilgisayar Mimarisi 9.32
MIPS subroutine return Jump register instruction (jr) Ornek jr $ra Address i $ra registerinde olan instruction a jump et. R formata sahip (memory address kismi yok) Eger MIPS code subroutine cagrimi yapmiyorsa Code u main label i ile baslat Code u ozel bir instruction ile sonlamaya gerek yok (veya jr $ra ile SPIM e geri don) $t0-$t9 haricinde bir register kullanmazsaniz, save islemi yapmaniza gerek yok 12/3/2004 Bilgisayar Mimarisi 9.33 12/3/2004 Bilgisayar Mimarisi 9.34 Eger kendi subroutinimizi cagriyorsak $ra register i SPIM e geri donus address ini tutar. Bizim fonksiyon cagriliz $ra nin icerigini degistirecek (SPIM e geri donus address ini yok edecek). Bu yuzden bizim subroutinimizi cagirmadan once satack e $ra push edilmeli ve SPIM e geri donulmeden once de push edilen geri donus addresi pop edilip $ra ya yazilmali 12/3/2004 Bilgisayar Mimarisi 9.35 Form to preserve proper return to SPIM.text # Program block starts here main: subu $sp,$sp,4 # Push return address on stack sw $ra, 0($sp) # since we will modify ra ###### Your code here, including jal MySub ###### lw $ra,0($sp) # Restore main's return address addi $sp,4 # Restore the stack pointer jr $ra # Return to Spim simulator ########## MySub here, in same source file 12/3/2004 Bilgisayar Mimarisi 9.36