1 / 30

CSC2510 - Computer Organization

CSC2510 - Computer Organization. Lecture 4: Machine Instructions and Programs Terrence Mak. Overview. Indexed addressing From assembly language to machine code I/O – data transfer Stack Subroutine. Move #LIST,R0. Clear. R1. Clear. R2. n. N. Clear. R3. Move.

libba
Download Presentation

CSC2510 - Computer Organization

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. CSC2510 - Computer Organization Lecture 4: Machine Instructions and Programs Terrence Mak

  2. Overview • Indexed addressing • From assembly language to machine code • I/O – data transfer • Stack • Subroutine

  3. Move #LIST,R0 Clear R1 Clear R2 n N Clear R3 Move N,R4 Student ID LIST Add 4(R0),R1 LOOP LIST + 4 Test 1 Add 8(R0),R2 A d d 12(R0),R3 LIST + 8 Test 2 Add #16,R0 LIST + 12 Test 3 Decrement R4 Branch>0 LOOP LIST + 16 Student ID Move R1,SUM1 Test 1 R2,SUM2 Move Move R3,SUM3 Test 2 Test 3 • • • Indexed Addressing Example Student 1 Student 2 • Indexed addressing is used to access operand whose location is defined relative to a given address

  4. Indexed Addressing Variations • Index • EA=[Ri]+X • Base with index • EA=[Ri]+[Rj] • Base with index and offset • EA=[Ri]+[Rj]+X

  5. Move N,R1 Initialization Move #NUM1,R2 Clear R0 LOOP Add (R2)+,R0 100 104 Decrement R1 108 Branch>0 LOOP Move R0,SUM Relative Addressing • Relative (offset from the PC) • Recall PC determines the address of the next instruction to execute • EA=[PC]+X • Used mainly for loops e.g. below, use relative address to get the update the PC. What is X?

  6. Additional modes • Autoincrement/autodecrement • (Ri)+ or –(Ri) • EA=[Ri]; increment Ri • decrement Ri; EA=[Ri] • Used to step through arrays, implement stacks etc • Increment/decrement amount depends on whether we are making byte, 16-bit or word accesses • Computers may have some of all of the modes discussed

  7. Assembly Language Assembler Assembly language move #5,R0 Machine code 89abffaa • We use mnemonics to express an assembly language in a symbolic manner • Assembly language is set of rules for using the mnemonics • Assembler translates assembly language to machine instructions called machine language • Program is a text file called a source program, assembled version is called an object program

  8. INSTRUCTION AMODE1 AMODE2 OPERAND1 OPERAND2 MOV REG ABS 0 SUM F 1 2 0 FF89 Opcodes • Mnemonic Move R0,SUM #bits 4 4 4 16 4

  9. Assembly language • Must have syntax to explain what mode is being used • E.g. ADD 5,R5 • does the 5 mean immediate or absolute? • ADD #5,R5 or ADDI 5,R5 • Indirect addressing normally specified by parentheses e.g. move #5,(R2) Immediate

  10. Assembler directives • STUDENTS EQU 20 • STUDENTS is a symbol meaning 20 i.e. a label • No machine code is generated for this directive • ORIGIN 200 • Specifies that the assembler should place machine code at address 200 • DATAWORD 100 • Data value 100 should be placed in memory • Labels allow symbolic references to memory addresses • Don’t need to know their actual value

  11. Memory Addressing address or data lab el Op eration information 100 Move N,R1 104 Move #NUM1,R2 Assem bler directiv es SUM EQU 200 108 Clear R0 ORIGIN 204 N D A T A W ORD 100 LOOP 112 Add (R2),R0 NUM1 RESER VE 400 116 Add #4,R2 ORIGIN 100 Statemen ts that ST AR T MO VE N,R1 120 Decrement R1 generate MO VE #NUM1,R2 124 Branch>0 LOOP mac hine CLR R0 128 Move R0,SUM instructions LOOP ADD (R2),R0 ADD #4,R2 132 DEC R1 BGTZ LOOP MO VE R0,SUM Assem bler directiv es RETURN SUM 200 END ST AR T N 204 100 NUM1 208 NUM2 212 NUM n 604 Figure 2.17. Memory arrangement for the program in Figure 2.12. Assembly language vs machine code

  12. Assembler • Has to know • How to interpret assembly language (directives, instructions, addressing modes etc) • Where to place the instructions in memory • Where to place the data in memory • Scans through source program, keeps track of all names and corresponding numerical values in symbol table e.g. what all the labels mean • Calculate branch addresses • Forward branch problem – how can it work out forward addresses?

  13. Two pass assembler • First pass • Work out all the addresses of labels • Second pass • Generate machine code, substituting values for the labels

  14. Loader • Transfers machine code from disk to memory • Execute first instruction

  15. Number notation • Differs with different assemblers • Need to be able to specify constants in binary, decimal, hex • ADD #93, R1 • ADD #%01011101, R1 • ADD #$5D, R1

  16. Basic I/O • I/O is the means by which data are transferred between the processor and the outside world • Devices operate at different speeds to the processor so handshaking is required

  17. Keyboard/displayExample • The keyboard and display are coordinated via software • Register (on device) assigned to the keyboard hardware • DATAIN contains ASCII of last typed character • SIN is the status control flag, normally 0. When a character typed, becomes 1. After the processor reads DATAIN, it is automatically set back to 0 • Register (on device) assigned to the display hardware • DATAOUT receives a character code • SOUT is the status control flag. It is 1 when ready to receive a character, set to 0 when the character is being transferred • These registers form the respective device interface

  18. Programmed IO READWAIT Branch to READWAIT if SIN=0 INPUT from DATAIN to R1 WRITEWAIT Branch to WRITEWAIT if SOUT=0 Output from R1 to DATAOUT

  19. Memory Mapped IO • On many machines, registers such as DATAIN, DATAOUT are memory-mapped • Read and write specific memory locations to communicate with device • MoveByte DATAIN,R1 • MoveByte R1,DATAOUT • SIN and SOUT might be bits in a device status register e.g. bit 3

  20. Memory-Mapped IO READWAIT Branch to READWAIT if SIN=0 INPUT from DATAIN to R1 READWAIT Testbit #3,INSTATUS Branch=0 READWAIT MoveByte DATAIN,R1 What about WRITEWAIT? WRITEWAIT Branch to WRITEWAIT if SOUT=0 Output from R1 to DATAOUT

  21. Mo v e #LOC,R0 Initialize p oin ter register R0 to p oin t to the address of the first lo cation in memory where the c haracters are to b e stored. READ T estBit #3,INST A TUS W ait for a c haracter to b e en tered Branc h=0 READ in the k eyb oard buffer D A T AIN. Mo v eByte D A T AIN,(R0) T ransfer the c haracter from D A T AIN in to the memory (this clears SIN to 0). ECHO T estBit #3,OUTST A TUS W ait for the displa y to b ecome ready . Branc h=0 ECHO Mo v eByte (R0),D A T A OUT Mo v e the c haracter just read to the displa y buffer register (this clears SOUT to 0). Compare #CR,(R0)+ Chec k if the c haracter just read is CR (carriage return). If it is not CR, then Branc h READ branc h bac k and read another c haracter. 0 Also, incremen t the p oin ter to store the next c haracter. Complete Example

  22. 0 • • • Stack pointer register Current SP - 28 top element 17 739 Stack • • • Bottom BOTTOM 43 element • • • k 2 - 1 Stacks • List of data elements (usually bytes or words) • Elements can only be removed at one end of the list • Last-in-first-out • Can be implemented in several ways, one way is • First element placed in BOTTOM • Grows in direction of decreasing memory address • Assume 32-bit data

  23. Stack Implementation Subtract #4,SP Move NEWITEM,(SP) ; push Move (SP),ITEM ; pop Add #4,SP With autoincrement and autodecrement Move NEWITEM,-(SP) ; push • How do you write pop using autoincrement? • How can I check that push/pop doesn’t overflow/underflow? SP 19 - 28 - 28 17 SP 17 739 739 • • • Stack • • • 43 43 NEWITEM 19 ITEM - 28 (a) After push from NEWITEM (b) After pop into ITEM

  24. SAFEPOP Compare #2000,SP Chec k to see if the stac k p oin ter con tains Branc h > 0 EMPTYERR OR an address v alue greater than 2000. If it do es, the stac k is empt y . Branc h to the routine EMPTYERR OR for appropriate action. Mo v e (SP)+,ITEM Otherwise, p op the top of the stac k in to memory lo cation ITEM. (a) Routine for a safe pop operation SAFEPUSH Compare #1500,SP Chec k to see if the stac k p oin ter  Branc h 0 FULLERR OR con tains an address v alue equal to or less than 1500. If it do es, the stac k is full. Branc h to the routine FULLERR OR for appropriate action. – Mo v e NEWITEM, (SP) Otherwise, push the elemen t in memory lo cation NEWITEM on to the stac k. Safe pop/push

  25. Similar data structures • Queue • First-in-first-out • Unlike a stack, need to keep track of both the front and end for removal and insertion respectively • Need two pointers to keep track of both ends • Assuming it moves through memory in direction of higher addresses, as it is used, it walks through memory towards higher addresses • Circular buffers avoid this problem by limiting to a fixed region in memory • Start at BEGINNING and entries appended until it reaches END after which it wraps back around to BEGINNING • Need to deal with cases when it is completely full and completely empty

  26. Subroutines • Often need to perform subtask on different data. Subtask called a subroutine • Rather than include the same sequence of instructions everywhere it is needed, call a subroutine instead • One copy of subroutine stored in memory • Subroutine call causes a branch to the subroutine • At the end of the subroutine, a return instruction is executed • Program resumes execution at the instruction immediately following the subroutine call

  27. Memory Memory location Calling program location Subroutine SUB 200 Call SUB 1000 first instruction 204 next instruction Return Subroutine call

  28. Memory Memory location Calling program location Subroutine SUB 200 Call SUB 1000 first instruction 204 next instruction Return 1000 PC 204 Link 204 Return Call Implementation • Since subroutine can be called from a number of different places in the program, need to keep track of the return address • Call instruction saves the contents of the PC • Simplest is a link register

  29. Call Sequence • Call • Store the contents of the PC in link register • Branch to target address specified in the instruction • Return • Branch to address contained in the link register What about the case of nested subroutines (i.e. a subroutine calls a subroutine)? What data structure do we need?

  30. Nested Subroutines • Call • Push the contents of the PC to the processor stack (pointed to by the stack pointer SP) • Branch to target address specified in the instruction • Return • Branch to address popped from processor stack

More Related