6 Text vs Data Assembly programs have two sections: Instructions go hereLarger of the two sectionsContains the beginning of the programDataWhere the variables are declaredA limited number of data types in ASMEverything is separated by tabs!
7 Declaring Data Done in the .data section <name>: .<type> <value>str: .asciiz “Hello World\0”str2: .ascii “Hello”number: .byte 10bigNum: .word
8 The RegistersNote: there are also 16 floating-point registers $f0-$f15
9 I/O Load one of these values into $v0 and arguments into registers $a0…$a3. $v0 is for system calls
10 Hello World It is very important that you understand when the syscall .text # instruction segment starts here.globl __start # tell where to start__start: # execution starts herela $a0, str # load the address of str into $a0li $v0, 4 # syscall 4 prints the string whose address is in $a0# look at chart!syscall # call syscallli $v0, 10 # syscall 10 exit program.data # data segment starts herestr: .asciiz "hello world\n" #define a null terminated string called strIt is very important that you understand when the syscallis executed, it will look in $a0 for what is to be printed
12 Multiplication/Division Because multiplication/division can result in a larger number, there are two special registers named lo and hi# move 5 into $t0li $t0, 5# move 2 into $t1li $t1, 2# mult, putting into lo & himult $t0, $t1# move from lo into $t3mflo $t3
13 .text # Fahrenheit to Celsius calculator .globl __start__start:la $a0, prompt # print prompt on terminalli $v0, 4syscallli $v0, 5 # read from usermul $t0, $v0, 9 # to convert, multiply by 9,div $t0, $t0, 5 # divide by 5, thenadd $t0, $t0, 32 # add 32la $a0, ans1 # print string before resultmove $a0, $t0 # print resultli $v0, 1la $a0, endl # system call to printli $v0, 4 # out a newlineli $v0, # exit program.data # data segmentprompt: .asciiz "Enter temperature (Celsius): "ans1: .asciiz "The temperature in Fahrenheit is: "endl: .asciiz "\n"
14 Control Structures j <addr> #update PC to be addr beq $t0, $t1, <addr> #if t0==t1, go to addrbeqz $t0, <addr> #if t0 == 0, go to addrbne $t0, $t1, <addr> #if t0 != t1, go to addrblt $t0, $t1, <addr> #if t0 < t1, go to addr“Mmmm.. blt…” – Homer Simpson
15 Labels Anywhere in our code, we can put a label Labels are names in the codeKeep us from having to know the exact memory address!Labels are followed by a colonmyLabel:
16 Count to 10 .text .globl __start __start: li $t0, 0 # $t0 will be our counter, starting at 0loop: # here is our label to jump to lateradd $t0, 1 # $t0 = $t0 + 1move $a0, $t0 # $a0 = $t0, necessary because $v0 works with $a0li $v0, 1 # what kind of syscall? Print int! Go back & look at chartsyscallla $a0, endl # print a return (\n)li $v0, 4bne $t0, 10, loop # if $t0 != 10, then branch to the loop labelli $v0, 4 # print “Thank you”la $a0, str.data # data sectionendl: .asciiz "\n";str: .asciiz "Thank you!\n"
17 Reading in Strings First, need to open up space for the string # open up 64 bytesmySpace: .space 64String goes into $a0, and length into $a1la $a0, mySpaceli $a1, 64Do system callli $v0, 8Now, mySpace is an array of characters!