Download presentation
Presentation is loading. Please wait.
1
Programming in Machine Language
CSCI 311 Dr. Frank Li
2
Index Overview Bit-wise Logical Operations
3
Brookshear Machine Architecture
4
Brookshear Machine Architecture
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
5
Instruction Format 3 5 A 7 0011 Op-code
16-bit patterns per instruction 0011 0101 1010 0111 Hex form 3 5 A 7 Operand Op-code
6
1RXY - Load register R with contents of location XY
0iii - No-operation 1RXY - Load register R with contents of location XY 2RXY - Load register R with value XY 3RXY - Store contents of register R at location XY 4iRS - Move contents of register R to register S 5RST - Add contents of registers S and T as binary numbers, place result in register R 6RST - Add contents of registers S and T as floating-point numbers, place result in register R 7RST - OR together the contents of registers S and T , place result in register R 8RST - AND together the contents of registers S and T , place result in register R 9RST - XOR together the contents of registers S and T , place result in register R ARiZ - Rotate the contents of register R one bit to the right, Z times BRXY - Jump to instruction at XY if contents of register R equal contents of register 0 Ciii - Halt DRXY - Jump to instruction at XY if contents of register R are greater than contents of register 0 R,S,T - Register numbers XY - A one-byte address or data value Z - A half-byte value i - Ignored when the instruction is de-coded: usually entered as 0
7
GUI: three major sections
the PC registers contain working data values as well as the Program Counter the Memory contains hexadecimal encoded instructions and data; the Run controls on the bottom, control the start, stop and speed of the simulation as well as allowing the user to reset the PC registers and Program Counter.
9
Example: Trace a program by hand
Address Contents 00 15 01 6c 02 16 03 6d 04 50 05 56 06 30 07 6e 08 c0 09 6a 6b 0b 0c Trace the following machine instruction by hand and write the results, given the contents of main memory as in the table 17
10
Index Overview Bit-wise Logical Operations
11
Bit-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. performed by aligning the bytes and performing the logical operations on the corresponding bits, one by one.
12
Bit-wise Logical Operations
AND OR XOR
13
Masking Goal: To test the individual pattern of bits in a given string of bits The sequence of bits that are used to examine a particular bit is known as the mask Using Mask, along with the appropriate logical operation, a programmer can determine the values of individual bits in a byte
14
Masking Technique 1: 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 leave the interesting bit and mask away the others Example: Suppose you want to determine if a number is odd or even.
15
Example 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 AND AND 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
16
Exercise 1 Suppose you have an 8-bit string (a byte) that is in 2’s 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?
17
Masking Technique 2: 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 OR
18
Exercise 2 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 , we want the result to be
19
Masking Technique 3: Re-Setting
Re-Setting (set to 0) a bit in a bit string AND with 0 in the bit position that needs resetting 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 AND
20
Converting ASCII Case 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. (See ASCII code at next page )
21
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z
22
Exercise 3: Converting ASCII Case
Task: Write a program to convert uppercase to lowercase for an ASCII character stored in a memory location 30
23
Combining Nybbles A Nybble (or, nibble) is a half-byte = 4 bits
given two bytes, 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 and , the answer would be Devise a sequence of logical operations using bit masks to do this.
24
Exercise 4: Combining Nybbles
Task: Develop a machine language program to combine two nybbles. combines the first half of the 1st byte (4 bits) with the last half of the 2nd byte (4 bits). The given two bytes are stored in locations 20 and 21, and the result byte should be stored in location 22.
25
Rotation: right and left “wrap around”
Rotate left 1 position Rotate left 1 position again Rotate right 1 position Rotate right 1 position again
26
Shift: right and left Shift right 1 position Shift left 1 position
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 Shift right 1 position again Shift left 1 position Shift left 1 position again
27
Arithmetic Shift A special form of shift, except the sign bit is preserved Arithmetic shift to the right by one position
28
Exercise 5: No Shift Instruction? Make one
The Brookshear machine does not have a SHIFT instruction, although there is a ROTATE How can a SHIFT be accomplished in the Brookshear machine? Demonstrate your answer with two examples.
29
Exercise 6: Trace a program
Trace the following instructions and explain what the program does. 04 132A 0A 342A 0C 4043 0E 9413 10 342B 12 C000 What are the contents of cells 2A and 2B when program halts? Suppose the cell addresses 08-0F contain the following data: Address Contents 28 2A 29 3D 2A 14 2B FF 2C F0 2D F1 2E F2 2F F3
30
Exercise 7: Trace a program
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 12: 03 Run this program stored in cells 00 through 12 with different value in location 12: 04 02 What does this program do? LOAD R0 00 LOAD R1 01 LOAD R3 01 LOADI R2 [12] JUMP R2 [10] //if R2 == R0 (is 0) jump to [10] (is halt) ROR R // R1 become 02 ADD R0 R0, R3 //R0 become 01
31
Exercise 8: Trace a program
Address Contents 00 10 01 20 02 11 03 21 04 22 05 06 26 07 08 23 09 0A 40 0B 56 0C 55 0D 61 0E 52 0F B2 14 12 B0 13 35 15 16 C0 17 Address Contents 20 03 21 02 22 00 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? 1020 1121 2200 2600 2301 4056 5561 5223 B214 B00A 3522 C000 0000 0302 ------ 06 08 0a
32
Exercise 9: adding three numbers
Task: Write a program to add three numbers together. The numbers are stored at memory cell locations 10, 11, and 12. The result should be stored at location 13. 1010 1111 5001 1112 3013 c000 0000 0102 0300
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.