Documente Academic
Documente Profesional
Documente Cultură
Think of assembly language as a very low level programming language. There are very few instructions. Many things that you can do in one step in high level languages you have to do in many steps Why learn Assembly Language? It is the language of the machine. Computers dont understand C or Java directly. Well see how we can implement assembly language. It helps you to understand how compilers work
Lec.03 - 288
Lec.03 - 289
Lec.03 - 290
Lec.03 - 291
Lec.03 - 292
Lec.03 - 293
Lec.03 - 294
Lec.03 - 295
Lec.03 - 296
Lec.03 - 297
Lec.03 - 298
Lec.03 - 299
Sometimes, especially when you are dealing strings you want to read and write bytes. The MIPS processor has two instructions lb and sb which read and write bytes, these have the same format as lw and sw.
Lec.03 - 300
Recap Load/Store
la to load an address into a register. Remember there is a distinction between the address and the value stored at that address. (Pointers and values). lw load a value into a register from memory, sw store a value and sw from a register to memory. Integer arrays, multiply by 4. Strings and byte arrays, use lb and sb.
Lec.03 - 301
Lec.03 - 302
MIPS Tutorial
Programs and data are in the same memory. The processor just fetches numbers from memory executes instructions. Almost every combination of bits is an instruction. Thus you could ask the machine to execute a sound file with unpredictable consequences. Also, the machine has no way of knowing if a value in memory is data or an instruction. So if you do a lw or a sw you better know what your reading or writing. All this has a positive side, you can write programs that write other programs (Can you think of any?). Your assembly language programs will fail in very strange ways, you better get used to understanding the code you write.
Lec.03 - 303
Lec.03 - 304
MIPS Tutorial
.data n1: .word 10 .text .globl main main: la $t0,n1 lw $s0,0($t0) addi $s0,$s0,1 sw $s0,0($t0) jr $31 main is the address of the first instruction. n1 is the address of a piece of data.
Lec.03 - 305
Lec.03 - 306
Lec.03 - 307
skip:
Lec.03 - 308
set_zero: skip2:
Jump instruction. This is like an unconditional branch : make the next instruction the label.
Lec.03 - 309
MIPS Tutorial
There is often more than one way of writing the same piece of code. For example: bne $s1,$s2,skip add $s3,$0,$0 Next Instruction
skip:
Can also be written as: beq $s1,$s2,settozero j skip add $s3,$0,$0 Next Instruction
settozero: skip:
Lec.03 - 310
exit:
Lec.03 - 312
Lec.03 - 313
MIPS Tutorial
Pseudo C code: if $s1 < $s2 then $s3 = 0 Assembly:
Lec.03 - 314
Lec.03 - 315
Lec.03 - 316
Lec.03 - 317
silly:
Lec.03 - 318
Lec.03 - 319
Lec.03 - 320
The important thing about a stack is that it is a LIFO (Last in First Out) data structure. This is useful for nested functions. You store your temporary data by pushing it onto the stack and restore things by popping things from it
Lec.03 - 321
Lec.03 - 322
Lec.03 - 323
silly:
# PUSH # ra # POP # ra
Lec.03 - 324
# # # #
POP ra POP s0
Lec.03 - 325
Lec.03 - 326
Lec.03 - 327
Example
Factorial
N! = N*(N-1)*(N-2)* * 1
Lec.03 - 328
Example: factorial
result: .space 4 #the place for the result .text .globl main main: addi $sp,$sp,-4 #save the return address. sw $ra,0($sp) lw $a0, 5 jal fact la $t0,result sw $v0, 0($t0) lw $ra,0($sp) addi $sp,$sp,4 jr $ra
Lec.03 - 329
Example: factorial
fact: addi $sp,$sp,-4 sw $ra,0($sp) #push $ra on the stack #fact of 0 is 1 bne $a0,$zero,not_zero #Set $v0 to be 1 addi $v0,$zero,1 #Restore $ra from the stack lw $ra,0($sp) #Read $ra from the stack addi $sp,$sp,4 #restore stack pointer. jr $ra
Lec.03 - 330
Example: factorial
not_zero: addi $sp,$sp,-4 sw $a0,0($sp) #push n on the stack ($a0=n) #So call fact with our new parameter addi $a0,$a0,-1 jal fact # $v0=fact(n-1) lw $t0,0($sp) #restore n from the stack. addi $sp,$sp,4 mul $v0,$v0,$t0 #$v0 = fact(n-1)($v0)*n($t0) #Restore the stack for $ra lw $ra,0($sp) addi $sp,$sp,4 jr $ra
Lec.03 - 331