29
1 Lab Session-IV CSIT-120 Lab Session-IV CSIT-120 Spring 2001 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine The “Micro” Simulator Lab 4 Continued

1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

  • View
    216

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

1

Lab Session-IV CSIT-120 Lab Session-IV CSIT-120 Spring 2001Spring 2001

• Lab 3 Revision and Exercises

• Rev: Precedence Rules

• Lab Exercise 4-A

• Machine Language Programming

• The “Micro” Machine

• The “Micro” Simulator

• Lab 4 Continued

Page 2: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

Rev: Increment OperatorRev: Increment Operator

• In Experiment 3.2, we have seen the use of the unary increment operator

• ==> number++; i++

• Similarly we have a unary decrement operator

• ==> number--, i--

• Instead of number=number-1;

Page 3: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

Rev: ASCII and Rev: ASCII and UNICODEUNICODE

• The alphabets and digits are represented by integer values and this mapping is called a code

• ASCII code was originally 7 bits (128 values)

• Values from 00 to 1F were reserved for special non-printable control characters

Page 4: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

Rev: ASCII and Rev: ASCII and UNICODEUNICODE

• For example, trying to print ASCII code 7 will ring a bell on the computer

• Example: cout<< “\7”

• To print spaces cout<<“\t”

• Up from 20 are the uppercase, lowercase letters and digits alongwith punctuation marks

Page 5: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

Rev: ASCII and Rev: ASCII and UNICODEUNICODE

• ASCII was not sufficient as more symbols were needed

• ASCII was revised to be “Latin-1”, an 8-bit code that can have 256 symbols

• Latin-1 can cover some European languages

• Computers are being used all over the world

• A unified coding system was needed

Page 6: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

6

Rev: ASCII and UNICODERev: ASCII and UNICODE

• A consortium developed a standard code called UNICODE.

• This code has 16 bits, thus 65,536 code points are possible

• World languages have 200,000 symbols so all cannot be accommodated

• Values from 0 to 255 map to Latin-1 or ASCII so changes are not felt in English

Page 7: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

7

Rev: ASCII and UNICODERev: ASCII and UNICODE

• UNICODE allocates code points to languages in an “official” way

• Number of code points given is more than the letters in each language to accommodate different forms of each letter

• Adding new words in English e.g. applets does not require new code points but adding new words in Japanese requires new points

Page 8: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

8

Rev: Uncover the ASCII codeRev: Uncover the ASCII code

• Experiment 3.5

• Why do we include <ctype.h>?

• What is toascii?

• What is toupper?

• What is tolower? (HINT: Use these functions in your program to find out)

Page 9: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

9

Rev: Simple Encryption Rev: Simple Encryption TechniquesTechniques

• Once you are able to process a text string, you can convert the characters to their ASCII values

• The ASCII values are numeric. You can modify these values so that no one can understand what is in the string

• Exercise 3-C Take a character from the user, add 8 to it and print it. (DEMO)

Page 10: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

10

Rev: Mixing Arithmetic Rev: Mixing Arithmetic OperationsOperations

• When we try to perform several arithmetic operations in one expression, the expression becomes quite complex

• For example, consider the following

• Q = (A+B*C)(A+B/C)

• Q = ((A+B)*C)((A+B)/C)???

• OR

• Q = (A+(B*C))(A+(B/C))???

Page 11: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

11

Rev: Using Precedence RulesRev: Using Precedence Rules

• We can use the precedence rules to get an expression evaluated as per our needs

• Following are the precedence rules in C++ for arithmetic operations

• Highest Priority is given to () (parenthesis)

• Multiplication (*) and Division (/) take precedence over addition (+) and subtraction (-)

• Assignment (=) is done at the end

• We should use parenthesis to make the expression clear

Page 12: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

12

Lab Exercise 4-A (Demo Lab Exercise 4-A (Demo Required)Required)

• Develop a program that asks the user to input the daily snowfall totals (in inches) for the last week of February in Chautaqua county. Your program then calculates and displays the average snowfall per day during the week.(BONUS FLAG: All calculations are done in one single statement)

Page 13: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

13

Machine Language Machine Language ProgrammingProgramming

• All programs written in C++, Java or any other user-level language are translated to the machine language after compilation

• Look at an example that shows how a C++ program line will be converted to the machine language

Page 14: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

14

C++ Program

My_var = this_data+next_data

Compilation and Linking

0001010100111111 //Load R5 from Memory 0001010000111110 //Load R4 from Memory0110011001010100 //Add R4,R5 & store result in R60011011000111101 //Store R6 into Memory

The Compilation of ProgramsThe Compilation of Programs

Page 15: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

15

Machine Language ProgramsMachine Language Programs

• A compiled and linked program is a series of machine language instructions and data

• Each processor has its own set of machine language instructions

• Can programs compiled for Pentium II run on Alpha workstation?

Page 16: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

16

The “Micro” MachineThe “Micro” Machine

• The Micro Machine has

> 256 Memory cells, each cell can hold 1 byte

> Memory addresses range 00-FF (two digits)

> 16 General Purpose Registers

> Register addresses range 0-F (one digit)

> Program Counter and Instruction Register

> 12 Machine Language Instructions (1-C)

Page 17: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

17

The “Micro” SimulatorThe “Micro” Simulator

• The “Micro” Simulator is a C++ Program• Download this program by visiting “The

Micro” link in the labs webpage of the course• Click on “micro” to download or cut & paste

this program and save as a C++ file• Double click on this file to run Visual C++• Compile and run the program

Page 18: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

18

The Micro SimulatorThe Micro Simulator

• The full screen display shows the contents of the memory from cell 00 to cell FF

• It also shows contents of registers R0 through R15

• PC and IR contents are visible too• The machine has several single-letter

commands• (4-A): Experiment 4.1 (PC incr?)

Page 19: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

19

The Instructions of “Micro”The Instructions of “Micro”

Page 20: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

20

Lab-4 ContinuedLab-4 Continued

• The Micro Machine and its Simulator

• An example program

• Exercises

• The JUMP instruction and its usage

• Experiment 4.4

Page 21: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

21

Micro and its SimulatorMicro and its Simulator

• What is the memory size in Micro?

• How many instructions are there?

• How can we start executing a program in Micro?

• What is the difference between S and G commands?

Page 22: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

22

How to Program the MicroHow to Program the Micro

• Let us walk through an example to learn how to program the Micro

• EXAMPLE: Write a program in Micro to add numbers stored in R1,R2 and R3 together and leave the result in R4

• This program will have two parts. The initialization part will store the values in registers and then the program will perform the desired operation

Page 23: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

23

ExampleExample

• INITIALIZATION

• //Move 5 into R1, (2RXY ==> 2105)

• //Move 8 into R2 (2RXY ==> 2208)

• //Move 7 into R3 (2RXY ==>2307)

• ACTUAL ADDITION

• //Add R1 and R2 and store the result at temporary location R5 (5RST ==>5512)

• //Add R5 and R3 and store the result in R4

• (5RST ==> 5453)

• //Stop the program (HALT C000)

Page 24: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

24

Example Program in Hex codeExample Program in Hex code

• 2105

• 2208

• 2307

• 5512

• 5453

• C000

Page 25: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

25

How to load the program?How to load the program?

• You cannot run your program until it is loaded in Micro’s memory

• Divide the memory into two equal blocks. First block should be reserved for data and second block for programs.

• Since the memory is 256 bytes (numbered from 0 to 255) exactly byte 0 to byte 127 are to be reserved for data

Page 26: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

26

How to load the program?How to load the program?

• Byte 128 onwards can be used for loading the programs

• Byte 128 has the binary address 1000 0000• It can be expressed in Hex as 80• Type M and give starting address as 80• When entering your program, only two digits

can be stored in each memory location

Page 27: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

27

How to run the program?How to run the program?

• Before running the program, you have to set the Program counter to point to the first instruction in the program

• Select P and enter 80

• Run the program all at once by typing G or one step at a time by typing S

• Do both one by one (First S then G)

Page 28: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

28

The Power to SimulateThe Power to Simulate

• The Micro Simulator can run programs designed by you too

• Lab Exercise 4-B• Design a program that adds numbers 1 through

5 together and leaves the result in register R9. (DEMO REQUIRED) (DATA SHOULD BE IN MEMORY OR REGISTERS)

• (4-C) Experiment 4.2

Page 29: 1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine

29

JUMP InstructionJUMP Instruction

• JUMP instruction is provided to facilitate the implementation of loops and branches

• Its format is B RXY (JUMP RXY)• It means “jump to location XY if R=R0

• Unconditional jump if R0 compared to itself

• Experiment 4.4