marie!simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfcomputersystems)and)networks)...
TRANSCRIPT
![Page 1: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/1.jpg)
ì Computer Systems and Networks ECPE 170 – Jeff Shafer – University of the Pacific
MARIE Simulator
![Page 2: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/2.jpg)
Schedule
ì Today – More assembly programming
ì Next Tuesday ì Meet in KH 203 (normal classroom) ì MARIE instrucKon decoding hardware ì Plus Quiz 3!
ì Quiz 3 topic: Assembly programming! ì I will give you Table 4.7 from the book…
ì Next Thursday – Begin Chapter 5 ì Closer look at instrucKon sets
Fall 2011 Computer Systems and Networks
2
![Page 3: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/3.jpg)
MARIE Programming
ì WriKng code in assembly can be very inKmidaKng at first (or always)
ì High-‐level languages are easier to code in because ì You’re more comfortable with them ì They take fewer steps
ì Treat the problem like a complier would ì Think about how to code in another language, like C++ ì Convert each construct into a series of MARIE
instrucKons
Fall 2011 Computer Systems and Networks
3
![Page 4: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/4.jpg)
MARIE Programming
ì What was easy about programming MARIE?
ì What was hard about programming MARIE?
ì What Ips would you share to other beginning assembly programmers?
Fall 2011 Computer Systems and Networks
4
![Page 5: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/5.jpg)
Assembly Pitfall – Program Organization
ì The assembler and CPU do exactly what we tell it. ì However, that doesn’t always mean it does what we
intend!
ì You must be very explicit when organizing your program
Fall 2011 Computer Systems and Networks
5
![Page 6: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/6.jpg)
Assembly Pitfall – Program Organization
ì What did the programmer intend for this code to do? ì Z=X+Y
ì What does the program actually do? ì Our variable X (0006) gets
interpreted as a JNS 6 instrucKon!
Fall 2011 Computer Systems and Networks
6
LOAD X ADD Y
X, DEC 6 Y, DEC -3 Z DEC 0 STORE Z HALT
![Page 7: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/7.jpg)
Assembly Pitfall – Program Organization
ì Code should always jump around any data
ì Compilers did this for you in COMP 51 ì AutomaKcally separate
code and data
Fall 2011 Computer Systems and Networks
7
LOAD X ADD Y JMP SKIP
X, DEC 6 Y, DEC -3 Z DEC 0 SKIP, STORE Z HALT
![Page 8: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/8.jpg)
ì Clever Tricks
Fall 2011 Computer Systems and Networks
8
![Page 9: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/9.jpg)
Clever Tricks – Memory Addresses
ì Think back to first MARIE program (Example 4.1)
ì ADDR variable holds the address of the element to be added ì It’s a pointer ì Value = 0x117
Fall 2011 Computer Systems and Networks
9
ORG 100 / Example 4.1 Load Addr /Load address of first number to be added Store Next /Store this address is our Next pointer Load Num /Load the number of items to be added Subt One /Decrement Store Ctr /Store this value in Ctr to control looping
Loop, Load Sum /Load the Sum into AC AddI Next /Add the value pointed to by location Next Store Sum /Store this sum Load Next /Load Next Add One /Increment by one to point to next address Store Next /Store in our pointer Next Load Ctr /Load the loop control variable Subt One /Subtract one from the loop control variable Store Ctr /Store this new value in loop control variable Skipcond 000 /If control variable < 0, skip next instruction Jump Loop /Otherwise, go to Loop Halt /Terminate program
Addr, Hex 117 /Numbers to be summed start at location 117 Next, Hex 0 /A pointer to the next number to add Num, Dec 5 /The number of values to add Sum, Dec 0 /The sum Ctr, Hex 0 /The loop control variable One, Dec 1 /Used to increment and decrement by 1
Dec 10 /The values to be added together Dec 15 Dec 20 Dec 25 Dec 30
This is loca*on 117
![Page 10: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/10.jpg)
Clever Tricks – Memory Addresses
ì What happens if the program changes? ì Say, we add a few instrucKons
ì We would have to find the new starKng address of the data, save its value in our assembly code, and re-‐run the assembler ì Annoying!
Fall 2011 Computer Systems and Networks
10
ADDR, HEX 125
![Page 11: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/11.jpg)
Clever Tricks – Memory Addresses
ì Perfect world: The assembler lets us use a label and fills in the address from the symbol table on pass 2 ì Unfortunately we don’t live in this perfect world! ì Assembler doesn’t do this directly, but we can fool it
into doing something similarly useful
ì SoluKon takes advantage of the fact that the instrucKon format always uses the lower 12 bits of each instrucKon for the address ì i.e. this soluKon works for MARIE, but not
necessarily other assembly languages
Fall 2011 Computer Systems and Networks
11
![Page 12: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/12.jpg)
Clever Tricks – Memory Addresses
ì What happens when these changes are made to the example program?
Fall 2011 Computer Systems and Networks
12
100 Load Addr 101 Store Next … 106 Addi Next … 111 Addr, JnS List 112 Next, Hex 0 … 116 One, Dec 1 117 List, Dec 10
![Page 13: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/13.jpg)
Clever Tricks – Memory Addresses
ì Recall the RTL for the ADDI instrucKon:
ì The third operaKon truncates the opcode ì MBR = 16 bits, but MAR = 12 bits
ì Result: MAR ends up with only the address of LIST
Fall 2011 Computer Systems and Networks
13
MAR ← X MBR ← M[MAR] MAR ← MBR MBR ← M[MAR] AC ← AC + MBR
![Page 14: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/14.jpg)
Clever Tricks – Memory Addresses
ì This trick works with all instrucKons ì JnS is safest since its opcode is 0 ì The resulKng value placed in memory is only the 12-‐
bit address
Fall 2011 Computer Systems and Networks
14
![Page 15: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/15.jpg)
Homework 4.33 Tips
ì Either use the JNS trick just shown, or write your program in two passes ì Pass 1: Write the code the traverses the linked list ì Run the assembler and look at memory addresses
where it placed your program ì Pass 2: Update the linked list memory addresses
based on the assembler lisKng file produced
Fall 2011 Computer Systems and Networks
15
![Page 16: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/16.jpg)
Clever Tricks – Faking LOADI
ì MARIE has LOAD-‐Indirect (LOADI) and STORE-‐Indirect (STOREI) instrucKons ì But clever programmers don’t need them!
ì How could I “emulate” the LOADI X instrucIon using several non-‐indirect MARIE instrucIons?
Fall 2011 Computer Systems and Networks
16
CLEAR / Put 0 in AC ADDI X / Add indirect value from Mem[Mem[X]]
![Page 17: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/17.jpg)
Clever Tricks – Faking STOREI
ì How could I “emulate” the STOREI X instrucIon using several non-‐indirect MARIE instrucIons? ì This is harder!
ì Idea: Take advantage of the stored program concept ì InstrucKons are just data
ì We need a sequence of instrucKons that construct a STORE instrucKon with the desired address
ì This would be a good applicaKon of a subrouKne ì Pass the value to store in AC, place the address in a
parameter variable
Fall 2011 Computer Systems and Networks
17
![Page 18: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/18.jpg)
Clever Tricks – Faking STOREI
ì Equivalent code to STOREI X:
Fall 2011 Computer Systems and Networks
18
LOAD STROPCODE / Get opcode ADD X / Combine addr STORE STOREI / Save
STOREI, HEX 0 / Data: build instruction / here, then execute it / Program continues here...
...
... STROPCODE, STORE 0 / Data: Just opcode / for store
![Page 19: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/19.jpg)
ì Subroutines
Fall 2011 Computer Systems and Networks
19
![Page 20: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/20.jpg)
Subroutines
ì Result = addOne(input1);
ì What do we need for a subrouIne? (i.e. funcIon) ì Arguments to the funcKon (i.e. input data) ì Return value from the funcKon ì A way to jump to the funcKon ì A way to return from the funcKon when finished
Fall 2011 Computer Systems and Networks
20
![Page 21: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/21.jpg)
Subroutine Example
Fall 2011 Computer Systems and Networks
21
Load Data / get value Store Arg1 / store value as argument
Jns AddOne / call subroutine Load Return / load subroutine return data Output / print it! Halt / terminate
Data, Dec 20 / original value / ** Subroutine ** AddOne, Dec 0 / return address placed here Load Arg1 / get argument Add One / increment it Store Return / save return value JumpI AddOne / return with value in a
Arg1, Dec 0 / Empty: subroutine argument Return, Dec 0 / Empty: subroutine return value
![Page 22: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/22.jpg)
Subroutine Example
ì Write a subrouKne which calculates the length of a null-‐terminated ASCII string ì What is an ASCII string? ì What is null terminated?
Fall 2011 Computer Systems and Networks
22
![Page 23: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/23.jpg)
Subroutine Example
ì Write a subrouKne which calculates the length of a null-‐terminated ASCII string
Fall 2011 Computer Systems and Networks
23
unsigned int strlen(char *str) { unsigned int len = 0; while (*str != 0) { str++; len++; } return len; }
![Page 24: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/24.jpg)
Subroutine Example
ì This subrouKne needs: ì A parameter which is the starKng address of the
string ì A variable to keep track of string length ì A loop which terminates when a 0 is found ì Inside the loop, increment the pointer and the
length count ì To return the length variable
Fall 2011 Computer Systems and Networks
24
![Page 25: MARIE!Simulator!ecs-network.serv.pacific.edu/.../2011-fall-ecpe-170/slides/11mariesimulator2.pdfComputerSystems)and)Networks) ECPE!170!–Jeff!Shafer!–University!of!the!Pacific!](https://reader034.vdocuments.us/reader034/viewer/2022050401/5f7ea1fcb8e06a3d8b6be842/html5/thumbnails/25.jpg)
Lab Exercises
ì Pick one of these, and demonstrate it by the end of lab ì Need to provide a sample “main program” that includes
calling the subrouKne
1. Write a subrouIne which searches an array for a parKcular value ì Inputs: number, starKng address of array, size of array ì Output: index of first match, -‐1 if not found
2. Write a subrouIne which makes a copy of a null-‐terminated string ì Inputs: addresses of source and desKnaKon strings
Fall 2011 Computer Systems and Networks
25