1 / 17

Chapter 2b - Assembly Language Programming and Control Logic

Chapter 2b - Assembly Language Programming and Control Logic. Thanks for all the Memory!. When 32 registers just won’t do. Many times (almost all the time, actually), you can’t fit all of your data into 32 measly registers. What do you do? Put some (most) of the data in main memory .

fabiana
Télécharger la présentation

Chapter 2b - Assembly Language Programming and Control Logic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2b - Assembly Language Programming and Control Logic

  2. Thanks for all the Memory! When 32 registers just won’t do. Many times (almost all the time, actually), you can’t fit all of your data into 32 measly registers. What do you do? Put some (most) of the data in main memory. Remember: Smaller is faster. Registers: Small and Fast Memory: Big and Slow In MIPS, all operations (i.e. arithmetic) are done on registers, only. Memory is used only for storing what won’t fit in registers.

  3. 0996 3288 1000 43 1004 234 Loading and Storing 44 So you’ve got some data in memory. Big deal. You need it in a register to do anything useful. You need to loada register with a value from memory. lw $10, 1000($0) # copy memory location 1000 to $10 Afterwards, $10 has the value 43 Load Word - Loads a whole 32-bit word This value is added to 1000 - for now, it is zero Say you’ve added 1 to register $10 (now it has the value 44). Now you want to put it back in memory again. You need to storethe register’s value back tomemory. sw $10, 1000($0) # copy $10 to memory location 1000

  4. OpcodeRSRTImmediate Data 3595 240 10001101001001010000 0000 1111 0000 Load/Store Format • What instruction format do LW and SW have? • lw $5, 240($9) # load M[240+$9] into $5 • Needs • Opcode • Source register ($9) • Immediate Operand (240) • Destination register ($5) • Hmmm, we’ve seen this before.... I-Type Instruction Opcodefor LW: 35 6 bits 5 bits 5 bits 16 bits Opcodefor SW: 43 ThinkRegularity!

  5. Aside: Load and Store Architectures • The only way to communicate with memory is through a LW or SW instruction • If you want to operate on memory, you have to use at least three instructions (usually) lw $15, 4500($0) # load M[4500] into $15 add $15, $15, $3 # add $3 to $15 sw $15, 4500($0) # store $15 back into M[4500] • It doesn’t have to be this way • Contrast this with the Motorola 68000 • ADD D3, 4500 ; add register D3 to M[4500] • Is the grass greener on the other side? • MIPS: Takes more, simpler instructions... RISC • MC68000: Takes fewer, complex instructions... CISC

  6. Assembler directives • Somehow, we’ve got to get data into memory • User input • Involves system calls (we’ll get to that later) • Constant data • Constant data is data that is in memory before our program starts executing • Machine-language instructions don’t give much help • The only way is to use Immediate instructions • The assembler helps us here! • Assembler directivesare special commands to the assembler. The most common directives put data into memory.

  7. Buffer: 00 00 00 01 Buffer+ 4: 00 00 00 02 .word Assembler Directive Buffer:.word 01, 02 Label: A name for this memory to go by. Acts as a variable name. Data to be stored. .word: Directive to store words in memory here. Remember: Words are 4 bytes each! Loads from Buffer+0 Loads from Buffer+4 lw $12, Buffer($0) # $12 <-- 00 00 00 01 addi $10, $0, 4 # $10 <-- 4 lw $13, Buffer($10) # $13 <-- 00 00 00 02

  8. The Assembler Location Counter The assembler keeps track of where to put things by using a location counter. The location counter just points to the memory location to put the “next” item. For this example, assume the location counter starts at 4000 Hex Constants a denoted by the “0x” prefix Loc. Ctr. Label Table 4004 4008 4000: buffer1: .word 12 buffer2: .word 3, 4, 0x20,0x5 add $9, $0, $0 firstld: lw $8, buffer1($9) addi $9, $9, 4 secld: lw $10, buffer2($9) 4004: buffer1 = 4000 4016 buffer2 = 4004 4012 4020: firstld = 4024 4024: secld = 4032 4028: 4032: Instructions – stored in ordinary memory. Execute by telling the CPU to start running at location 4020.

  9. borg: 10 12 22 33 ? ? 1 8 greeting: 69 73 65 52 ... 0 2E 65 6C greeting2: 20 75 6F 59 ... -- 2E 64 65 Other Memory Assembler Directives borg: .byte 33, 22, 12, 10, 8, 1 .byte - reserves bytes in memory .asciiz - reserves Null-terminated ASCII chars greeting: .asciiz “Resistance is futile.” i s e R . e l Null-terminated .ascii - reserves ASCII characters (no NULL) greeting2: .ascii “You will be assimilated.”

  10. Meeting all your needs for space Sometimes, we need to allocate (empty) space to be used later. inputbuffer: .space 100 Allocates 100 bytes of space for an input buffer. If the space is to be used as words (with LW,SW), make sure it is aligned to a multiple of 22 using the .align directive .align 2inputbuffer: .space 100 Space allocated this way is just reserved by the assembler.You have to make your own use of it. addi $12, $0, 6 sw $12, inputbuffer($0) # stores 6 in buffer

  11. Our first program! # This is our first program! Yeah! .data Tonto: .word 0x44, 0x22 .text main: add $9, $0, $0 # clear $9 lw $8, Tonto($9) # put Tonto[0] in $8 addi $9, $9, 4 # increment $9 lw $10, Tonto($9) # put Tonto[1] in $10 addi $v0,$0,10 syscall .datameans that data follows .textmeans that code follows main:tells SPIM where to start these two instructions end the program

  12. SPIM • The SPIM simulator is a MIPS simulator • Allows development and debugging of MIPS programs without having to run on a MIPS CPU • QTSPIM (newer, many platforms) or PCSPIMat http://sourceforge.net/projects/spimsimulator/files/ • Help on SPIM • Appendix A of your textbook • Handouts and sample programs • Look under Resources on the course web page

  13. Pseudoinstructions • Some “missing” instructions are commonly composed of others • The assembler “implements” these by allowing the “missing” instructions to be entered in assembly code. • When machine code is generated, the pseudoinstructions are converted to real instructions. move $5, $3 add $5, $3, $0 neg $8, $9 sub $8, $0, $9 li $8, 44 addi $8, $0, 44 or ori $8, $0, 44

  14. Pseudoinstructions for branches • Branches can be nasty to figure out • SPIM provides several pseudoinstructions for branches blt $3, $4, dest slt $1, $3, $4bne $1, $0, dest bge $3, $4, dest slt $1, $3, $4beq $1, $0, dest $3 >= $4 is the opposite of $3 < $4 bgt $3, $4, dest slt $1, $4, $3bne $1, $0, dest $3 > $4 same as$4 < $3 ble $3, $4, dest slt $1, $4, $3beq $1, $0, dest $3 <= $4 is the opposite of $3 > $4

  15. ActionCode (in $v0)Parameters Print an Integer 1 $a0 = value to print Print a String 4 $a0 = location of string Input an Integer 5 (after syscall) $v0 contains integer Input a String 8 $a0 = location of buffer, $a1 = length Exit program 10 SPIM I/O • SPIM I/O uses the SYSCALL pseudoinstruction • Set up parameters • Place correct code in $v0 • Execute SYSCALL To display a stringprompt: .asciiz “hello world” la $a0,prompt li $v0, 4 syscall To print the value in $t3: move $a0, $t3 li $v0, 1 syscall

  16. 2000 2004 2008 2012 2016 2020 2024 2028 Data Structures - Arrays A single-dimensional array (vector) is a simple linear data structure int A[5]; /* integers are 4 bytes each */ start of array (2004 in this example) A[0] A[1] A[2] A[3] For 4-byte integers: Location of A[n] = Start + n*4; A[4] For data items of size s bytes: Location of A[n] = Start + n*s; To declare an array named A with 40 bytes: A: .space 40 A is the address of element 0. The program must do all computationsneeded to access any other elements.

  17. ... 6000 List[0] 123 6004 List[1] 3288 6008 List[2] 43 List[3] 6012 1 6016 List[4] 45 ... Accessing data structures in memory Assume that the variable List points to the beginning of an arrayof 32-bit integers. List=6000 Move List[0] into $3: lw $3, List($0) # $3 <-- List[0] Move List[1] into $4: addi $8, $0, 4 # $8 <-- 4 lw $4, List($8) # $4 <-- List[1] Note: Memory addresses refer to 8-bit bytes! We usually reference 32-bit words. All lw/sw instructions must use an address that is a multiple of 4! To get proper index, have to multiply by 4. List and contents of $8 are added together to form address Move List[4] into $5: addi $8, $0, 16 # $8 <-- 16 lw $5, List($8) # $5 <-- List[4]

More Related