Introduction to CS – Sheet 2 : Data Manipulation PDF
Document Details
Uploaded by EnergyEfficientHarpy5322
Cairo University
ING005
Tags
Summary
This document is a tutorial on data manipulation in computer science. It covers topics such as the computer architecture, machine language, and logical operations. It includes questions and answers.
Full Transcript
Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulat...
Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation 1.Read Data: Retrieve the data from the source memory cell. 2.Store Temporarily: Hold the data in a temporary register or cache. 3.Write Data: Place the data into the destination memory cell. 4.Validate: Optionally, check that the data was transferred correctly. Introduction to CS – ING005 Sheet 2 : Data Manipulation 1.Memory Address: Specifies where to write the data. 2.Data: The actual information to be written. 3.Control Signal: Instructs the memory to perform a write operation. 4.Timing Information: Ensures synchronization and data stability during the write. Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation 1.Data is Copied, Not Physically Moved: The data is duplicated to a new location while often remaining in the original location. 2.No Physical Movement: Data transfer doesn't involve physical movement but replicates electronic or magnetic patterns. 3.Misleading: It might imply the original data is automatically erased or moved, which isn’t always true. In essence, "copy" or "transfer" might be more accurate terms, as they don't imply automatic deletion or physical movement of the original data. Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation 2356 A503 Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation The instruction register is used to hold the instruction being executed. The program counter contains the address of the next instruction to be executed, Introduction to CS – ING005 Sheet 2 : Data Manipulation Since each instruction in our machine is two bytes long, this fetch process involves retrieving the contents of two memory cells from main memory. The CPU places the instruction received from memory in its instruction register and then increments the program counter by two so that the counter contains the address of the next instruction stored in memory Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation Let's step through the execution: 1.Instruction 1 (00 01): 14 02 Op-code: 1 - LOAD the register R with the bit pattern found in the memory cell whose address is XY. Operand: 4 02 - Register 4, Memory Address 02 Register 4 gets the value from memory address 02, which is 34. 2.Instruction 2 (02 03): 34 17 Op-code: 3 - STORE the bit pattern found in register R in the memory cell whose address is XY. Operand: 4 17 - Register 4, Memory Address 17 Value from register 4 (which is 34 based on previous instruction) is stored in memory address 17. 3.Instruction 3 (04 05): C0 00 Op-code: C - HALT execution. Operand: 000 - N/A The machine halts execution. Result: When the machine halts, memory address 17 contains the bit pattern 34 Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation Let's go through the questions: a. If the program counter starts at B0, what bit pattern is in register number 3 after the first instruction has been executed? Instruction at B0, B1: 13 B8 Op-code: 1 - LOAD the register R with the bit pattern found in the memory cell whose address is XY. Operand: 3 B8 - Register 3, Memory Address B8 So, we load register 3 with the bit pattern found at memory address B8, which is 0F. Answer a: The bit pattern in register 3 is 0F after the first instruction has been executed. Introduction to CS – ING005 Sheet 2 : Data Manipulation b. What bit pattern is in memory cell B8 when the halt instruction is executed? Instruction at B2, B3: A3 02 Op-code: A - ROTATE the bit pattern in register R one bit to the right X times. Operand: 3 02 - Register 3, Rotate 2 times Rotate the bit pattern 0F (found in register 3 from step a) two times to the right. 0F in binary is 00001111, so rotating it twice to the right gives 11000011, which is C3 in hex. Instruction at B4, B5: 33 B8 Op-code: 3 - STORE the bit pattern found in register R in the memory cell whose address is XY. Operand: 3 B8 - Register 3, Memory Address B8 Store the bit pattern from register 3, which is now C3, into memory address B8. Instruction at B6, B7: C0 00 Op-code: C - HALT execution. Operand: 000 - N/A The machine halts execution. Answer b: The bit pattern in memory cell B8 is C3 when the halt instruction is executed Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation Let's step through the instructions considering that the program counter initially contains A4. a. What is in register 0 the first time the instruction at address AA is executed? Instruction at A4, A5: 20 00 Op-code: 2 - LOAD the register R with the bit pattern XY. Operand: 0 00 - Register 0, Bit pattern 00 Register 0 gets the value 00. Answer a: Register 0 contains 00 the first time the instruction at address AA is executed. Introduction to CS – Sheet 2 : Data Manipulation ING005 b. What is in register 0 the second time the instruction at address AA is executed? Let's continue with the next instructions: Instruction at A6, A7: 21 03 Op-code: 2 - LOAD the register R with the bit pattern XY. Operand: 1 03 - Register 1, Bit pattern 03 Register 1 gets the value 03. Instruction at A8, A9: 22 01 Op-code: 2 - LOAD the register R with the bit pattern XY. Operand: 2 01 - Register 2, Bit pattern 01 Register 2 gets the value 01. Instruction at AA, AB: B1 B0 Op-code: B - JUMP to the instruction located in the memory cell at address XY if the bit pattern in register R is equal to the bit pattern in register number 0. Operand: 1 B0 - Register 1, Memory address B0 Since register 0 has 00 and register 1 has 03, they are not equal, so we do not jump and continue to the next instruction. Next instructions: Instruction at AC, AD: 50 02 Op-code: 5 - ADD the bit patterns in registers S and T as though they were two’s complement representations and leave the result in register R. Operand: 0 02 - Register 0, Registers 0 and 2 Register 0 now gets the value 00 + 01 = 01. Answer b: Register 0 contains 01 the second time the instruction at address AA is executed. Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – ING005 Sheet 2 : Data Manipulation 11101011 11010010 Introduction to CS – ING005 Sheet 2 : Data Manipulation Example Byte: a b c d e f g h (where a, b, c, d, e, f, g, and h are bits, it could be 0 or 1) Goal: Keep the middle four bits (c d e f) and set others to 0. Mask: 00111100 Operation: Bitwise AND Introduction to CS – ING005 Sheet 2 : Data Manipulation a. Relationship with Number of 1s If you XOR the first two bits of a string of bits and then continue down the string by successively XORing each result with the next bit in the string, the final result is related to the parity of the number of 1s in the string. Specifically: If the number of 1s in the string is even, the result of this repeated XOR operation will be 0. If the number of 1s in the string is odd, the result will be 1. Introduction to CS – ING005 Sheet 2 : Data Manipulation Introduction to CS – Sheet 2 : Data Manipulation ING005 65 97 66 98 67 99 x AND 1 = x, x OR 0 = x and x OR 1 = 1 If you observe the ASCII values, you’ll notice that the bit that differentiates uppercase from lowercase letters is the 6th bit. For lowercase letters, it is set to 1, and for uppercase, it is set to 0. 11011111 MASK AND OR 00100000 MASK Introduction to CS – ING005 Sheet 2 : Data Manipulation First shift: 00110101 First shift: 10111111 Second shift: 10011010 Second shift: 11011111 Third shift: 01001101 Third shift: 11101111 Introduction to CS – ING005 Sheet 2 : Data Manipulation a. AB (in binary: 10101011) d. 35 (in binary: 00110101) Saving the leftmost bit: 1 Saving the leftmost bit: 0 Shifting all bits to the left: 01010110 Shifting all bits to the left: 01101010 Placing the saved bit on the right: 01010111 Placing the saved bit on the right: 01101010 Converting back to hexadecimal: 57 Converting back to hexadecimal: 6A Introduction to CS – ING005 Sheet 2 : Data Manipulation 8 bits >> 3 right shift positions=5 left shift positions 01110101 10111010 11101010 01011101 11010101 10101110 10101011 01010111 10101110 Introduction to CS – Sheet 2 : Data Manipulation ING005 One Program is as follow: 1 1A7: Load value from A7 into register 1. 2 280: Load mask 80 into register 2. x OR 0 = x 7 312: OR register 1 and register 2, store result in register 3. 3 3A7: Store value from register 3 into memory address A7. Step 1: Load the Current Value We'll load the current value at address A7 into a register (let's use register 1). Instruction: 1 1A7 Step 2: Preparing the Mask We need a mask to modify the MSB without changing other bits. A suitable mask is 10000000 (hex: 80). We'll load this value into another register (let's use register 2). Instruction: 2 280 Step 3: OR Operation Perform an OR operation between the register containing the original value and the register containing the mask. The result will be stored in a new register (let's use register 3). Instruction: 7 312 Step 4: Store the Result Store the new value (with the MSB set to 1) back to the memory cell at address A7. Instruction: 3 3A7 Introduction to CS – ING005 Sheet 2 : Data Manipulation MSB stands for "Most Significant Bit." In the context of binary numbers, it is the bit position in a binary number having the greatest value. Typically, in a binary number, the leftmost bit is the MSB.