.text assembly instructions follow .data data follows .globl globally visible label = symbolic address Hello World! .text # code section .globl main main: li $v0, 4 string # system call for print la $a0, str
# load address of string to syscall # print the string li $v0, 10 # system call for exit syscall # exit print .data str: .asciiz Hello world!\n # NUL terminated string, as Addressing modes lw $s1, addr
# load $s1 from addr lw $s1, 8($s0) # $s1 = Mem[$s0+8] register $s0 contains the base address access the address ($s0) possibly add an offset 8($s0) Load and move instructions la $a0, addr $a0 li $a0, 12 = 12 # load address addr into # load immediate $a0 lb $a0, c($s1) # load byte $a0 = Mem[$s1+c] lh $a0, c($s1) # load half word lw $a0, c($s1) # load word
Control Structures Assembly language has very few control structures: Branch instructions Jump instructions if cond then goto label goto label We can build while loops, for loops, repeat-until loops, if-then-else structures from these primitives Branch instructions beqz $s0, label if $s0==0 goto label bnez $s0, label if $s0!=0
goto label bge $s0, $s1, label goto label if $s0>=$s1 ble $s0, $s1, label goto label if $s0<=$s1 blt $s0, $s1, label beq $s0, $s1, label label if $s0<$s1 goto label if $s0==$s1 goto
if-then-else structures if ($t0==$t1) then /* blockA */ else /* blockB */ beq $t0, $t1, blockA j blockB blockA: instructions of then block j exit blockB: instructions of else block exit: subsequent instructions repeat-until loop repeat until $t0>$t1 loop: instructions of loop ble $t0, $t1, loop # if $t0<=$t1 goto loop Other loop structures are similar Exercise: Derive templates for various loop structures System calls
load argument registers load call code syscall li $a0, 10 # load argument $a0=10 li $v0, 1 # call code to print integer syscall # print $a0 SPIM system calls procedure print int code $v0 argument 1 $a0 contains number print float
2 print double print 3 4 $f12 contains number $f12 contains number $a0 address of SPIM system calls procedure code $v0 result read int 5 res returned in $v0 read float
6 res returned in $f0 read 7 res returned in $f0 double read string 8 Example programs Loop printing integers 1 to 10 1 2 3 Increasing array elements by 5 for(i=0; i
Print numbers 1 to 10 main: loop: li $s0, 1 # $s0 = loop counter li $s1, 10 # $s1 = upper bound of loop move $a0, $s0 # print loop counter $s0 li $v0, 1 syscall li $v0, 4 la $a0, linebrk # print \n
# linebrk: .asciiz \n syscall addi $s0, $s0, 1 # increase counter by 1 ble $s0, $s1, loop li $v0, 10 syscall # if ($s0<=$s1) goto loop # exit Increase array elements by 5 .text .globl main main: loop: la
$t0, Aaddr # $t0 = pointer to array A lw $t1, len # $t1 = length (of array A) sll $t1, $t1, 2 # $t1 = 4*length add $t1, $t1, $t0 # $t1 = address(A)+4*length
$t0, $t1, loop .data Aaddr: .word 0,2,1,4,5 len: .word 5 # array with 5 elements Increase array elements by 5 .text .globl main main: la $t0, Aaddr array A lw $t1, len sll $t1, $t1, 2 (byte addr.)
# $t0 = pointer to # $t1 = length (of array A) # $t1 = 4*length add $t1, $t1, $t0 # $t1 = beyond last elem. A Increase array elements by 5 Loop: lw $t2, ($t0) # $t2 = A[i] addi $t2, $t2, 5 # $t2 = $t2 + 5 sw $t2, ($t0) # A[i] = $t2
addi $t0, $t0, 4 # i = i+1 bne $t0, $t1, loop # if $t0<$t1 goto loop li $v0, 10 syscall # exit Increase array elements by 5 .data Aaddr: len: .word 0,2,1,4,5
.word 5 Idiosyncratic: Byte addressing => loop in steps of 4 Describe meaning of registers in your documentation! Procedures jal addr store address + 4 into $ra jump to address addr jr $ra
allows subroutine to jump back care must be taken to preserve $ra! more work for non-leaf procedures Procedures one of the few means to structure your assembly language program small entities that can be tested separately can make an assembly program more readable recursive procedures Write your own procedures
# prints the integer contained in $a0 print_int: li $v0, 1 # system call to syscall # print integer jr $ra # return main: . . . li $a0, 10 # we want to print 10 jal print_int # print integer in $a0
Write your own procedures .data linebrk: .asciiz \n .text print_eol: # prints "\n" li $v0, 4 # la $a0, linebrk # syscall # jr $ra # return
main: . . . jal print_eol # printf(\n) Write your own procedures .data main: loop: li $s0, 1 # $s0 = loop ctr li $s1, 10 # $s1 = upperbnd move $a0, $s0 # print loop ctr
Suppose that a procedure procA calls another procedure jal procB Problem: jal stores return address of procedure procB and destroys return address of procedure procA Save $ra and all necessary variables onto the stack, call procB, and retore Stack The stack can be used for high address parameter passing storing return addresses storing result variables
jal fib # move $s0, $v0 # $s0 = fib(10) fib is a recursive procedure with one argument $a0 need to store argument $a0, temporary register $s0 for intermediate results, and return address $ra fib: gen: rreg: sub $sp,$sp,12
# save registers on stack sw $a0, 0($sp) # save $a0 = n sw $s0, 4($sp) # save $s0 sw $ra, 8($sp) # save return address $ra bgt $a0,1, gen # if n>1 then goto generic case move $v0,$a0 # output = input if n=0 or n=1 j rreg
# goto restore registers sub $a0,$a0,1 # param = n-1 jal fib # compute fib(n-1) move $s0,$v0 # save fib(n-1) sub $a0,$a0,1 # set param to n-2 jal fib # and make recursive call
jr $ra # decrease the stack size Practice, practice, practice!!! Read Chapter 3 and Appendix A Write many programs and test them Get a thorough understanding of all assembly instructions Study the register conventions carefully
Definition: a main idea or an underlying meaning of a literary work that may be stated directly or indirectly. **Avoid confusing THEME with a SUBJECT. Subject is a topic which acts as a foundation for a literary work, while a...
Florida was Cuba's largest U.S. state trade partner. 40 percent of all cargo being routed through Miami's customs district was transported to Cuba. 85 percent of Cuba's exports were transported to the United States. IMPACT OF FREE TRADE WITH CUBA...
"Sai" refers to age. So if you know Japanese numbers you can say how old you are. Just add "sai" on to the end of the number. The only exceptions are the numbers that end with "chi", for example "ich"...
The children then begin their writing and are given regular opportunities to ensure their work is covering the regular VCOP. How do we start? We start with a stimulus, and this might be: A book (fiction or non-fiction) Poem ....
Ready to download the document? Go ahead and hit continue!