Example 1: (expression evaluation)

Slides:



Advertisements
Similar presentations
MIPS Assembly Tutorial
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Lecture 5: MIPS Instruction Set
Princess Sumaya Univ. Computer Engineering Dept. Chapter 2: IT Students.
CEG 320/520: Computer Organization and Assembly Language Programming1 Assembly Language Programming Machine Code Hand Assembly.
1 COMS 361 Computer Organization Title: Instructions Date: 9/28/2004 Lecture Number: 10.
Assembly Process. Machine Code Generation Assembling a program entails translating the assembly language into binary machine code This requires more than.
LC-3 Computer LC-3 Instructions
Chapter 3 Assembly Language: Part 1. Machine language program (in hex notation) from Chapter 2.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
Chapters 5 - The LC-3 LC-3 Computer Architecture Memory Map
CS 300 – Lecture 6 Intro to Computer Architecture / Assembly Language Instructions.
CEG 320/520: Computer Organization and Assembly Language Programming1 Assembly Language Programming Assembler Directives and The Symbol Table.
Dr Masri Ayob TK 2633: Microprocessor & Interfacing Lecture 7: Assembly Language.
CoE3DJ4 Digital Systems Design
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 1 In-Class Lab Session (Lab 2)
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Ass. Prof. Dr Masri Ayob TK 6123 Lecture 13: Assembly Language Level (Level 4)
Lecture 4: MIPS Instruction Set
How Computers Work Lecture 3 Page 1 How Computers Work Lecture 3 A Direct Execution RISC Processor: The Unpipelined BETA.
Computer Systems – Machine & Assembly code. Objectives Machine Code Assembly Language Op-code Operand Instruction Set.
Lecture 16: 10/29/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Midterm Exam Review. Data representation 2 For parts a-c: Fill in the missing values of this chart. All binary and hexadecimal values are given in 8-bit.
First Foray into Programming (the hard way). A reminder from last lesson: A machine code instruction has two parts:  Op-code  Operand An instruction.
SRC: instruction formats Op-coderarb rc c Type D Op-code Type Aunused Op-codera Type Bc1 21 Op-coderarb.
PROGRAMMING THE BASIC COMPUTER
Lec 3: Data Representation
Chapter 7 Assembly Language
Instruction Execution (Load and Store instructions)
A Uni-bus Data Path Implementation for the SRC
Chapter 11 Instruction Sets
COMPUTER ARCHITECTURE & OPERATIONS I
Structural RTL for the br and brl instructions
Assembly Language Assembly Language
Morgan Kaufmann Publishers
Lecture 4: MIPS Instruction Set
Chapter 7 Assembly Language
Computer Science 210 Computer Organization
Instruction Execution (Load and Store instructions)
External CPU Bus Activity
The University of Adelaide, School of Computer Science
Chapter 5 The LC-3.
Falcon-E : Introduction
Computer Architecture
Lecture 4: MIPS Instruction Set
Computer Programming Machine and Assembly.
Assembler CASE Tool.
MISP Assembly.
Chap. 6 Programming the Basic Computer
Computer Architecture & Operations I
The University of Adelaide, School of Computer Science
Data Transfers To be able to implement
Instruction encoding We’ve already seen some important aspects of processor design. A datapath contains an ALU, registers and memory. Programmers and compilers.
Instructions Instructions (referred to as micro-instructions in the book) specify a relatively simple task to be executed It is assumed that data are stored.
Instruction encoding The ISA defines Format = Encoding
Programmer’s View of the EAGLE
Computer Science 210 Computer Organization
Data manipulation instructions
LC-2: The Little Computer 2
Instruction encoding We’ve already seen some important aspects of processor design. A datapath contains an ALU, registers and memory. Programmers and compilers.
Execution time Execution Time (processor-related) = IC x CPI x T
RTL for the SRC pipeline registers
MIPS assembly.
Reverse Assembly Typical problem:
CS501 Advanced Computer Architecture
Reverse Assembly Typical problem:
Problem: Consider the following two SRC code segments for implementing multiplication. Find which one is more efficient in terms of instruction count and.
MIPS instructions.
Location Counter (LC) = 0 while line of code <> END if ORG
Presentation transcript:

Example 1: (expression evaluation) Write an SRC assembly language program to evaluate the expression: z = 4(a +b) – 16(c+58) Your code should not change the source operands Solution: Notice that the SRC does not have a multiply instruction. We may solve the problem by assuming that a multiply instruction, similar to the add instruction, exists. Instead, we will make use of the fact that multiplication with powers of 2 can be achieved by repeated shift left operations. A possible solution is give below: ld R1, c ; c is a label used for a memory location addi R3, R1, 58 ; R3 contains (c+58) shl R7, R3, 4 ; R7 contains 16(c+58) ld R4, a ld R5, b add R6, R4, R5 ; R6 contains (a+b) shl R8, R6, 2 ; R8 contains 4(a+b) sub R9, R7, R8 ; the result is in R9 st R9, z ; store the result in memory location z Note: the memory labels a,b,c and z can be defined by using assembler direectives like .dw or .db, etc. in the source file.

Another solution … Note: If we assume a mul instruction in the instruction set of the SRC, the shl will be replaced by the mul instruction as shown below: ld R1, c ; c is a label used for a memory location addi R3, R1, 58 ; R3 contains (c+58) mul R7, R3, 4 : R7 contains 16(c+58) ld R4, a ld R5, b add R6, R4, R5 ; R6 contains (a+b) mul R8, R6, 2 ; R8 contains 4(a+b) sub R9, R7, R8 ; the result is in R9 st R9, z ; store the result in memory location z Note: the memory labels a,b,c and z can be defined by using assembler direectives like .dw or .db, etc. in the source file.

Example 2: (hand assembly) Convert the given SRC assembly language program in to an equivalent SRC machine language program. ld R1, c ; c is a label used for a memory location addi R3, R1, 58 ; R3 contains (c+58) shl R7, R3, 4 : R7 contains 16(c+58) ld R4, a ld R5, b add R6, R4, R5 ; R6 contains (a+b) shl R8, R6, 2 ; R8 contains 4(a+b) sub R9, R7, R8 ; the result is in R9 st R9, z ; store the result in memory location z Solution: Note: This program uses memory labels a,b,c and z. We need to define them for the assembler by using assembler direectives like .dw or .equ, etc. in the source file.

Source program with directives .ORG 200 ; start the next line at address 200 a: .DW 1 ; reserve one word for the label a in the memory b: .DW 1 ; same for b… this will be at address 204 c: .DW 1 ; the 32-bit memory word c will be at address 208 z: .DW 1 ; reserve one word for the result also ; .ORG 400 ; start the code at address 400 ; all numbers are in decimal unless otherwise stated ld R1, c ; c is a label used for a memory location addi R3, R1, 58 ; R3 contains (c+58) shl R7, R3, 4 : R7 contains 16(c+58) ld R4, a ld R5, b add R6, R4, R5 ; R6 contains (a+b) shl R8, R6, 2 ; R8 contains 4(a+b) sub R9, R7, R8 ; the result is in R9 st R9, z ; store the result in memory location z This is the way a program will appear in the source file. Most assemblers require that the file be saved with a .asm extension.

Solution: .ORG 200 ; start the next line at address 200 a: .DW 1 ; reserve one word for the label a in the memory b: .DW 1 ; same for b… this will be at address 204 c: .DW 1 ; the 32-bit memory word c will be at address 208 z: .DW 1 ; reserve one word for the result also ; .ORG 400 ; start the code at address 400 We conclude the following from the above statements: Label Address Value a 200 unknown b 204 c 208 z 212

Solution: (continued…) The code starts at address 400 and each instruction takes 32 bits in the memory. A memory map for the program will be as shown on the next slide

Memory Map for the SRC example program Memory Address Memory Contents 200 unknown 204 208 212 … ,,, 400 ld R1, c 404 addi R3, R1, 58 408 shl R7, R3, 4 412 ld R4, a 416 ld R5, b 420 add R6, R4, R5 424 shl R8, R6, 2 428 sub R9, R7, R8 432 st R9, z Memory Map for the SRC example program

Notice that this is a type C instruction with the rb field missing 400 ld R1, c Notice that this is a type C instruction with the rb field missing

1. Pick op code corresponding to ld from SRC table 400 ld R1, c 00001 la 1 lar ld ldr neg nop not or ori shc shl shr shra st stop str sub 1. Pick op code corresponding to ld from SRC table

2. Pick register code corresponding to R1 from register table 400 ld R1, c 00001 la 1 lar ld ldr neg nop not or ori shc shl shr shra st stop str sub 00001 1. Pick op code corresponding to ld from SRC table Register Code R0 00000 R8 01000 R16 10000 R24 11000 R1 00001 R9 01001 R17 10001 R25 11001 R2 00010 R10 01010 R18 10010 R26 11010 R3 00011 R11 01011 R19 10011 R27 11011 R4 00100 R12 01100 R20 10100 R28 11100 R5 00101 R13 01101 R21 10101 R29 11101 R6 00110 R14 01110 R22 10110 R30 11110 R7 00111 R15 01111 R23 10111 R31 11111 Notice this is the type C format

3. Notice that there is no register coded in the rb field 3. Notice that there is no register coded in the rb field. So, use 5 0’s 2. Pick register code corresponding to R1 from register table 400 ld R1, c 00001 la 1 lar ld ldr neg nop not or ori shc shl shr shra st stop str sub 00001 1. Pick op code corresponding to ld from SRC table 00000 Register Code R0 00000 R8 01000 R16 10000 R24 11000 R1 00001 R9 01001 R17 10001 R25 11001 R2 00010 R10 01010 R18 10010 R26 11010 R3 00011 R11 01011 R19 10011 R27 11011 R4 00100 R12 01100 R20 10100 R28 11100 R5 00101 R13 01101 R21 10101 R29 11101 R6 00110 R14 01110 R22 10110 R30 11110 R7 00111 R15 01111 R23 10111 R31 11111 Notice this is the type C format

3. Notice that there is no register coded in the rb field 3. Notice that there is no register coded in the rb field. So, use 5 0’s 2. Pick register code corresponding to R1 from register table 400 ld R1, c 00001 la 1 lar ld ldr neg nop not or ori shc shl shr shra st stop str sub 00001 1. Pick op code corresponding to ld from SRC table 00000 Register Code R0 00000 R8 01000 R16 10000 R24 11000 R1 00001 R9 01001 R17 10001 R25 11001 R2 00010 R10 01010 R18 10010 R26 11010 R3 00011 R11 01011 R19 10011 R27 11011 R4 00100 R12 01100 R20 10100 R28 11100 R5 00101 R13 01101 R21 10101 R29 11101 R6 00110 R14 01110 R22 10110 R30 11110 R7 00111 R15 01111 R23 10111 R31 11111 00000 0000 1101 0000 Notice this is the type C format 4.The value of the label c is provided by the assembler, and should be converted to 17 bits Label Address Value a 200 unknown b 204 c 208 z 212

3. Notice that there is no register coded in the rb field 3. Notice that there is no register coded in the rb field. So, use 5 0’s 2. Pick register code corresponding to R1 from register table 400 ld R1, c 00001 la 1 lar ld ldr neg nop not or ori shc shl shr shra st stop str sub 00001 1. Pick op code corresponding to ld from SRC table 00000 Register Code R0 00000 R8 01000 R16 10000 R24 11000 R1 00001 R9 01001 R17 10001 R25 11001 R2 00010 R10 01010 R18 10010 R26 11010 R3 00011 R11 01011 R19 10011 R27 11011 R4 00100 R12 01100 R20 10100 R28 11100 R5 00101 R13 01101 R21 10101 R29 11101 R6 00110 R14 01110 R22 10110 R30 11110 R7 00111 R15 01111 R23 10111 R31 11111 00000 0000 1101 0000 Notice this is the type C format 4.The value of the label c is provided by the assembler, and should be converted to 17 bits Label Address Value a 200 unknown b 204 c 208 z 212 00001 00001 00000 00000 0000 1101 0000 5. The complete instruction

3. Notice that there is no register coded in the rb field 3. Notice that there is no register coded in the rb field. So, use 5 0’s 2. Pick register code corresponding to R1 from register table 400 ld R1, c 00001 la 1 lar ld ldr neg nop not or ori shc shl shr shra st stop str sub 00001 1. Pick op code corresponding to ld from SRC table 00000 Register Code R0 00000 R8 01000 R16 10000 R24 11000 R1 00001 R9 01001 R17 10001 R25 11001 R2 00010 R10 01010 R18 10010 R26 11010 R3 00011 R11 01011 R19 10011 R27 11011 R4 00100 R12 01100 R20 10100 R28 11100 R5 00101 R13 01101 R21 10101 R29 11101 R6 00110 R14 01110 R22 10110 R30 11110 R7 00111 R15 01111 R23 10111 R31 11111 00000 0000 1101 0000 Notice this is the type C format 4. Finally, the value of the label c is provided by the assembler, and should be converted to 17 bits Label Address Value a 200 unknown b 204 c 208 z 212 00001 00001 00000 00000 0000 1101 0000 5. The complete instruction 6. And in hexadeximal 0 8 4 0 0 0 D 0 h

Memory Map for the SRC example program with hex codes included Memory Address Memory Contents Hexadecimal Memory Contents 200 unknown 204 208 212 … 400 ld R1, c 084000D0 h 404 addi R3, R1, 58 408 shl R7, R3, 4 412 ld R4, a 416 ld R5, b 420 add R6, R4, R5 424 shl R8, R6, 2 428 sub R9, R7, R8 432 st R9, z Memory Map for the SRC example program with hex codes included

Type C instruction with the rc field missing 404 addi R3, R1, 58 Type C instruction with the rc field missing

1. Pick op code corresponding to addi from SRC table 404 addi R3,R1, 58 01101 la 1 lar ld ldr neg nop not or ori shc shl shr shra st addi str sub 1. Pick op code corresponding to addi from SRC table

404 addi R3,R1,58 2. Pick register code corresponding to R3 from register table 01101 la 1 lar ld ldr neg nop not or ori shc shl shr shra addi stop str sub 00011 00001 1. Pick op code corresponding to addi from SRC table 1. Pick op code corresponding to addi from SRC table Register Code R0 00000 R8 01000 R16 10000 R24 11000 R1 00001 R9 01001 R17 10001 R25 11001 R2 00010 R10 01010 R18 10010 R26 11010 R3 00011 R11 01011 R19 10011 R27 11011 R4 00100 R12 01100 R20 10100 R28 11100 R5 00101 R13 01101 R21 10101 R29 11101 R6 00110 R14 01110 R22 10110 R30 11110 R7 00111 R15 01111 R23 10111 R31 11111 2. Pick register code corresponding to R1 from register table

4. Notice that there is no register coded in the rc field 4. Notice that there is no register coded in the rc field. Use five zeros instead. 404 addi R3,R1,58 2. Pick register code corresponding to R3 from register table 01101 la 1 lar ld ldr neg nop not or ori shc shl shr shra addi stop str sub 00011 00001 1. Pick op code corresponding to addi from SRC table Register Code R0 00000 R8 01000 R16 10000 R24 11000 R1 00001 R9 01001 R17 10001 R25 11001 R2 00010 R10 01010 R18 10010 R26 11010 R3 00011 R11 01011 R19 10011 R27 11011 R4 00100 R12 01100 R20 10100 R28 11100 R5 00101 R13 01101 R21 10101 R29 11101 R6 00110 R14 01110 R22 10110 R30 11110 R7 00111 R15 01111 R23 10111 R31 11111 3. Pick register code corresponding to R1 from register table

4. Use the binary code for immediate data 404 addi R3,R1,58 2. Pick register code corresponding to R3 from register table 01101 la 1 lar ld ldr neg nop not or ori shc shl shr shra addi stop str sub 00011 00001 1. Pick op code corresponding to addi from SRC table Register Code R0 00000 R8 01000 R16 10000 R24 11000 R1 00001 R9 01001 R17 10001 R25 11001 R2 00010 R10 01010 R18 10010 R26 11010 R3 00011 R11 01011 R19 10011 R27 11011 R4 00100 R12 01100 R20 10100 R28 11100 R5 00101 R13 01101 R21 10101 R29 11101 R6 00110 R14 01110 R22 10110 R30 11110 R7 00111 R15 01111 R23 10111 R31 11111 00000 0000 0011 1010 3. Pick register code corresponding to R1 from register table

4. Use the binary code for immediate data 404 addi R3,R1,58 2. Pick register code corresponding to R3 from register table 01101 la 1 lar ld ldr neg nop not or ori shc shl shr shra st stop str sub 00011 00001 1. Pick op code corresponding to addi from SRC table Register Code R0 00000 R8 01000 R16 10000 R24 11000 R1 00001 R9 01001 R17 10001 R25 11001 R2 00010 R10 01010 R18 10010 R26 11010 R3 00011 R11 01011 R19 10011 R27 11011 R4 00100 R12 01100 R20 10100 R28 11100 R5 00101 R13 01101 R21 10101 R29 11101 R6 00110 R14 01110 R22 10110 R30 11110 R7 00111 R15 01111 R23 10111 R31 11111 00000 0000 0011 1010 3. Pick register code corresponding to R1 from register table 01101 00011 00001 00000 0000 0011 1010

4. Use the binary code for immediate data 404 addi R3,R1,58 2. Pick register code corresponding to R3 from register table 01101 la 1 lar ld ldr neg nop not or ori shc shl shr shra st stop str sub 00011 00001 1. Pick op code corresponding to addi from SRC table Register Code R0 00000 R8 01000 R16 10000 R24 11000 R1 00001 R9 01001 R17 10001 R25 11001 R2 00010 R10 01010 R18 10010 R26 11010 R3 00011 R11 01011 R19 10011 R27 11011 R4 00100 R12 01100 R20 10100 R28 11100 R5 00101 R13 01101 R21 10101 R29 11101 R6 00110 R14 01110 R22 10110 R30 11110 R7 00111 R15 01111 R23 10111 R31 11111 0000 0011 1010 3. Pick register code corresponding to R1 from register table 5. The complete instruction 01101 00011 00001 00000 0000 0011 1010 6 8 C 2 0 0 3 A h 6. And in hexadecimal

Memory Map for the SRC example program with hex codes included Memory Address Memory Contents Hexadecimal Memory Contents 200 unknown 204 208 212 … 400 ld R1, c 084000D0 h 404 addi R3, R1, 58 68C2003A h 408 shl R7, R3, 4 412 ld R4, a 416 ld R5, b 420 add R6, R4, R5 424 shl R8, R6, 2 428 sub R9, R7, R8 432 st R9, z Memory Map for the SRC example program with hex codes included

Memory Map for the SRC example program with hex codes included Memory Address Memory Contents Hexadecimal Memory Contents 200 unknown 204 208 212 … 400 ld R1, c 084000D0 h 404 addi R3, R1, 58 68C2003A h 408 shl R7, R3, 4 E1C60004 h 412 ld R4, a 090000C8 h 416 ld R5, b 094000CC h 420 add R6, R4, R5 61885000 h 424 shl R8, R6, 2 E20C0002 h 428 sub R9, R7, R8 724E8000 h 432 st R9, z 1A4000D4 h Memory Map for the SRC example program with hex codes included

Precisely speaking!!! Memory Address Memory Contents Hexadecimal ..... ….. 400 08 h 401 40 h 402 00 h 403 D0 h 404 68 h 405 C2 h 406 407 3A h 408 E1 h 409 C6 h 410 411 04 h 412 09 h 413 414 415 C8 h 416 417 Precisely speaking!!! Memory Address Memory Contents Hexadecimal Memory Contents 400 ld R1, c 084000D0 h 404 addi R3, R1, 58 68C2003A h 408 shl R7, R3, 4 E1C60004 h 412 ld R4, a 090000C8 h 416 ld R5, b 094000CC h 420 add R6, R4, R5 61885000 h 424 shl R8, R6, 2 E20C0002 h 428 sub R9, R7, R8 724E8000 h 432 st R9, z 1A4000D4 h

Review

Example 3: ( SRC instruction analysis) Identify the formats of following SRC instructions and specify the values in the fields Instruction format ra rb rc c1 c2 c3 neg r1, r2 add r0,r2,r3 nop ld r2,6 shl r0,r1,3

Solution: Instruction format ra rb rc c1 c2 c3 neg r1, r2 D r1 _ r2 add r0,r2,r3 r0 r3 nop A ld r2,6 C 6 shl r0,r1,3 3