Title: Brookshear Machine
1Brookshear Machine
- Programming
- in
- Machine Language
2Brookshear Machine
- 16 general-purpose registers
- numbered 0 through 15
- Hex notation numbered 0 through F
- 256 byte-size main memory cells (i.e., 8 bits
each) - numbered 0 through 255
- Hex notation numbered 00 through FF
- 12 simple instructions
- encoded using 16 bits (2 bytes) per instruction
- Hex notation 4 hex digits per instruction
- One hex digit for op-code
- Other three hex digits for Operands
3Brookshear Machine Architecture
4Brookshear Instruction Format
Actual 16-bit patterns per instruction
0011
0101
1010
0111
Hex form (4 bits)
3
5
A
7
Operand
Op-code
5Brookshear Machine Instructions
- Op-code Operation
- 1 LOAD contents
- 2 LOAD value
- 3 STORE
- 4 MOVE
- 5 ADD 2s complement
- 6 ADD floating point
- 7 OR
- 8 AND
- 9 XOR
- A Rotate
- B Jump
- C Halt
6Exercise trace by hand
- Using Appendix C, trace the following machine
instruction by hand and write the results, given
the contents of main memory as in the table - 156C
- 166D
- 5056
- 306E
- C000
Main memory
7BSML In-class Exercises
- Textbook page 81 1, 7
- Textbook page 86 1, 2
- Textbook page 100 8
8Bit-wise Logical Operations
- A byte can be seen as a set of 8 Boolean
variables (each bit is one variable) - Logical instructions like AND, OR, XOR, etc. are
performed by aligning the bytes and performing
the logical operations on the corresponding bits,
one by one.
9Bit-wise Logical Operations
- AND OR
- 1001001 11100001
- 1110001 10001100
- 1000001 11101101
- XOR
- 100101001
- 111001101
- 011100100
10Masking
- To test the individual pattern of bits in a given
string of bits - Using particular sequences of bits as the Mask,
along with the appropriate logical operation, a
programmer can determine the values of individual
bits in a byte - The sequence of bits that are used to examine a
particular bit is known as the mask
11Masking Techniques Reading
- Reading a bit in a bit string is done by masking
away the bits we are not interested in - AND operator along with a bit mask of 1 in the
position we want to read will leave the
interesting bit and mask away the others - Example Suppose you want to determine if a
number is odd or even. The low-order bit
(rightmost binary digit) is 1 in an odd number
and 0 in an even number. A mask of 0001 with
AND operator will test the last bit - Even Number Odd Number
- 100101010 1010100101
- AND 000000001 AND 0000000001
- 000000000 0000000001
- By examining the result of the masking operation,
you can determine the number as odd or even If
the result is 0, it is even
12Question
- Suppose you have an 8-bit string (a byte) that
is in 2s complement notation, sitting in a
memory cell. You want to determine if it
represents a positive or negative number. What
bit-mask would you use? What Logical operation
would you use?
13Masking Techniques Setting
- Setting (set to 1) a bit in a bit string is done
by - an OR operation with 1 in the position we want to
set to 1, 0 in the other positions leaving the
other bits unchanged. - Example Suppose you want to set the high-order
bit to a 1 in a given bit string - 00100110
- OR 10000000
- 10100110
14Question
- Suppose you want to set the 2nd bit from left in
a given bit string to 1. What is the bit mask you
would use? and what is the operation to achieve
the result? - i.e., given the bit string 10000010, we want the
result to be 11000010.
15Masking Techniques Re-Setting
- Re-Setting (set to 0) a bit in a bit string is
done by - AND with 0 in the bit position that needs
resetting, and 1 in the other positions in order
not to change the other bits - Example Suppose you want to reset the high-order
bit to a 0 in a given bit string - 10100110
- AND 01111111
- 00100110
16Converting ASCII Case
- Purpose Work with bit masks
- Who Groups of 2 3
- Task Develop a mask and select operator to
convert uppercase ASCII characters to lowercase,
e.g. A to a, B to b, etc. - Product Mask and operator. Spokesperson will
present groups finding - Time 5 minutes
- (See Appendix A for ASCII code reference )
17ASCII code reference
18Algorithm for converting ASCII case
- First, pick any alphabet and notice the bit
pattern match for lower and upper case in ASCII.
Do the same with as many letters of the alphabet
as you want, till you notice the pattern - Then, use appropriate mask and logical operator
to set the appropriate bit for the lowercase
ASCII bits
19Combining Nybbles
- A Nybble (or, nibble) is a half-byte 4 bits
- Purpose Develop a sequence of logical operations
using bit masks - Who Groups of 2-3
- Task You are given two bytes. You must create a
third byte that combines the first half of the
1st byte (4 bits) with the last half of the 2nd
byte (4 bits). - For example, given 01101001 and 11100111, the
answer would be 01100111. Devise a sequence of
logical operations using bit masks to do this. - Product Masks and operations
- Time 5 minutes
20Algorithm for Combining Nybbles
- Using appropriate mask and logical operator, read
the first 4 bits of first byte and store the
result as result1 - Using appropriate mask and logical operator, read
the last 4 bits of the second byte and store the
result as result2 - Combine result1 and result2 using appropriate
logical operator to set the final result
21Rotation right and leftwrap around
- Rotate left 1 position
- 01001010 ? 10010100
- Rotate left 1 position again
- 10010100 ? 00101001
- Rotate right 1 position
- 01001010 ? 00100101
- Rotate right 1 position again
- 00100101 ? 10010010
22Shift right and left
- Shifting is similar to rotation, except the bits
fall off the end instead of wrap around and
you fill in the gap with 0. - Shift right 1 position
- 11001010 ? 01100101
- Shift right 1 position again
- 01100101 ? 00110010
- Shift left 1 position
- 11001010 ? 10010100
- Shift left 1 position again
- 10010100 ? 00101000
Left fill with 0
Right fill with 0
23Arithmetic Shift
- A special form of shift, except the sign bit is
preserved - Arithmetic shift to the right by one position
- 11001001 ? 10100100
Preserve the sign bit
Left fill with 0 after shifting
24No Shift Instruction?
- Extra Credit Question
- Notice that the Brookshear machine does not have
a SHIFT instruction, although there is a ROTATE - How can a SHIFT be accomplished in the Brookshear
machine? - Think about it later. Write your answers on a
sheet of paper. Demonstrate your answer with two
examples to get credit. You can turn it in
anytime before final exam.
25Hand Trace
- Purpose Trace some machine language programs
- Who Groups of 2-3
- Task What is the result of executing the
following programs? - Product Describe the state of the machine after
execution (memory and register contents).
Spokesperson presents results. - Time 10 min
26Hand Trace Exercises
- Program 1 what is the contents of cell address
10 when program halts? - 2005
- 2106
- 5201
- 3210
- C000
- Program 2 Suppose the cell addresses 08-0F
contain the following data - Address Contents
- 08 2A09 3D0A 140B FF0C F00D F10E F2
0F F3
- Program 2 continued
- Trace the following instructions and explain what
the program does. - 1108
- 1209
- 130A
- 5412
- 5443
- 340A
- 4043
- 9413
- 340B
- C000
- What are the contents of cells 0A and OB when
program halts?
27Program Execution
- Use the Brookshear Machine Simulator at the
following site - http//spot.pcc.edu/spreuitt/CS160/Simulators/BSM
achine/machinegui.html
28Simulator trace Exercise 1 (5 mins)
- Address contents
- 00 20
- 01 00
- 02 21
- 03 01
- 04 23
- 05 01
- 06 12
- 07 12
- 08 B2
- 09 10
- 0A A1
- 0B 07
- 0C 50
- 0D 03
- 0E B0
- 0F 08
- 10 C0
- 11 00
- Run this program stored in cells 00 through 12
with different value in location 12 - 04
- 02
- What does this program do?
29Simulator Trace Exercise 2 (5 min)
Try the following values in cells 20 and 21. In
each case, write down the contents of cell 22
when the program completes execution. Case
1 cell 20 contains 04 and cell 21
contains 02 Case 2 cell 20 contains 05 and
cell 21 contains 02 What does this program do?
30Write a Program in Brookshear Machine Language
(BSML)
- Purpose Write a BSML program
- Who Groups of 2
- Task Write a program to add three numbers
together. The numbers are stored at memory cell
locations 00, 01, and 02. The result should be
stored at location 03. - Product A program to test on the simulator
- Time 5 minutes
31One Answer
- 00 ??
- 01 ??
- 02 ??
- 03 ??
- 04 10
- 05 00
- 06 11
- 07 01
- 08 50
- 09 01
- 0A 11
- 0B 02
- 0C 50
- 0D 01
- 0E 30
- 0F 03
- 10 C0
- 11 00
32Write a BSML Program Nybbles
- Purpose Write a BSML program
- Who Groups of 2
- Task Develop a machine language program to
combine nybbles. The given bytes are stored in
locations 00 and 01, and the result should be
stored in location 02. - Product Program to test on the simulator
- Time 10 minutes
33Write a BSML Program ASCII case
- Purpose Write in BSML program
- Who Groups of 2
- Task Write a program to convert uppercase to
lowercase for an ASCII character stored in a
memory location - Product Program to test on the simulator
- Time 10 minutes