Bit Manipulation
Bit Manipulation
10
5 A gardener grows vegetables in a greenhouse. For the vegetables to grow well, the temperature
needs to always be within a particular range.
The gardener is not sure about the actual temperatures in the greenhouse during the growing
season. The gardener installs some equipment. This records the temperature every hour during
the growing season.
...............................................................................................................................................[1]
(b) Identify three items of hardware that would be needed to acquire and record the temperature
data. Justify your choice for each.
Item 1 ........................................................................................................................................
Justification ...............................................................................................................................
...................................................................................................................................................
Item 2 ........................................................................................................................................
Justification ...............................................................................................................................
...................................................................................................................................................
Item 3 ........................................................................................................................................
Justification ................................................................................................................................
...............................................................................................................................................[6]
(c) The equipment records temperatures in the greenhouse. It does this for seven locations.
Each recording is stored as two successive bytes. The format is shown below:
7 6 5 4 3 2 1 0
Byte 1 Byte 2
The location is indicated by the setting of one of the seven bits in byte 1. For example,
location 4 is indicated by setting bit 4.
7 6 5 4 3 2 1 0
0 0 1 0 0 0 0 1 0 0 0 1 1 0 0 0
Byte 1 Byte 2
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[2]
Complete the boxes below to show the two bytes for this recording. The reading has not
yet been processed.
7 6 5 4 3 2 1 0
Byte 1 Byte 2
[2]
(d) (i) The accumulator is loaded with the value of byte 1 from location 106.
Write the assembly language instruction to check whether the reading in byte 2 came
from location 4.
.......................................................................................................................................[4]
(ii) Write the assembly language instruction to set the flag (bit 0) of the byte contained in the
accumulator to 1.
.......................................................................................................................................[2]
6 A company grows vegetables in a number of large greenhouses. For the vegetables to grow well,
the temperature, light level and soil moisture need to always be within certain ranges.
The company installs a computerised system to keep these three growing conditions within the
best ranges. Sensors are used for collecting data about the temperature, light level, and moisture
content of the soil.
...............................................................................................................................................[1]
(b) Give three items of hardware that would be needed for this system. Justify your choice.
Do not include sensors in your answer.
Item 1 ........................................................................................................................................
Justification ...............................................................................................................................
...................................................................................................................................................
Item 2 ........................................................................................................................................
Justification ...............................................................................................................................
...................................................................................................................................................
Item 3 ........................................................................................................................................
Justification ...............................................................................................................................
...............................................................................................................................................[6]
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[3]
(ii) When the system was designed, various parameters for temperature were set.
...........................................................................................................................................
.......................................................................................................................................[1]
(iii) Explain how this parameter value is used by the feedback system.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[2]
• A flag is set to indicate whether its associated sensor reading is waiting to be processed.
• More than one sensor reading may be waiting to be processed at any particular moment.
• Data received from the sensors is stored in a block of eight consecutive bytes (addresses
201–208).
• The data from sensor 1 is at address 201, the data from sensor 2 is at address 202, and
so on.
Sensor number
1 2 3 4 5 6 7 8
150 0 1 0 0 0 1 0 1
201 0 0 0 0 0 0 0 0
202 0 0 0 0 0 1 0 0
203 0 0 0 0 0 0 0 0
204 0 0 0 1 0 0 0 0
205 0 0 0 0 0 0 1 0
206 0 0 0 1 0 1 0 0
207 0 0 0 1 0 0 1 0
208 0 0 0 1 0 0 1 0
...........................................................................................................................................
.......................................................................................................................................[2]
(ii) The accumulator is loaded with the data from location 150.
Write the assembly language instruction to check whether there is a value waiting to be
processed for sensor 6.
.......................................................................................................................................[3]
BLANK PAGE
2 A compiler uses a keyword table and a symbol table. Part of the keyword table is shown below.
Keyword Token
01
+ 02
= 03
IF 4A
THEN 4B
ENDIF 4C
ELSE 4D
FOR 4E
STEP 4F
TO 50
INPUT 51
OUTPUT 52
ENDFOR 53
Entries in the symbol table are allocated tokens. These values start from 60 (hexadecimal).
Counter 1.5
INPUT Num1
// Check values
IF Counter = Num1
THEN
Num1 Num1 + 5.0
ENDIF
(a) Complete the symbol table below to show its contents after the lexical analysis stage.
Token
Symbol
Value Type
Counter 60 Variable
1.5 61 Constant
[3]
© UCLES 2015 9608/31/O/N/15
5
(b) Each cell below represents one byte of the output from the lexical analysis stage.
Using the keyword table and your answer to part (a) complete the output from the lexical
analysis.
60 01
[2]
A B + C + D
After the syntax analysis stage, the compiler generates object code. The equivalent code, in
assembly language, is shown below:
(i) Name the final stage in the compilation process that follows this code generation stage.
.......................................................................................................................................[1]
(ii) Rewrite the equivalent code given above to show the effect of it being processed through
this final stage.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[2]
(iii) State two benefits of the compilation process performing this final stage.
Benefit 1 ............................................................................................................................
...........................................................................................................................................
Benefit 2 ............................................................................................................................
.......................................................................................................................................[2]
2 In this question, you are shown pseudocode in place of a real high-level language. A compiler
uses a keyword table and a symbol table. Part of the keyword table is shown below.
Keyword Token
01
+ 02
= 03
IF 4A
THEN 4B
ENDIF 4C
ELSE 4D
FOR 4E
STEP 4F
TO 50
INPUT 51
OUTPUT 52
ENDFOR 53
Entries in the symbol table are allocated tokens. These values start from 60 (hexadecimal).
Start 0.1
// Output values in loop
FOR Counter Start TO 10
OUTPUT Counter + Start
ENDFOR
(a) Complete the symbol table below to show its contents after the lexical analysis stage.
Token
Symbol
Value Type
Start 60 Variable
0.1 61 Constant
[3]
© UCLES 2015 9608/32/O/N/15
5
(b) Each cell below represents one byte of the output from the lexical analysis stage.
Using the keyword table and your answer to part (a) complete the output from the lexical
analysis.
60 01
[2]
(c) The compilation process has a number of stages. The output of the lexical analysis stage
forms the input to the next stage.
.......................................................................................................................................[1]
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[2]
(d) The final stage of compilation is optimisation. There are a number of reasons for performing
optimisation. One reason is to produce code that minimises the amount of memory used.
.......................................................................................................................................[1]
A B + 2 * 6
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[1]
X A + B
Y A + B + C
Following the syntax analysis stage, object code is generated. The equivalent code, in
assembly language, is shown below:
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[3]
6 An intruder detection system for a large house has four sensors. An 8-bit memory location stores
the output from each sensor in its own bit position.
(a) (i) State the name of the type of system to which intruder detection systems belong.
.......................................................................................................................................[1]
...........................................................................................................................................
.......................................................................................................................................[1]
(b) Name two sensors that could be used in this intruder detection system. Give a reason for
your choice.
Sensor 1 ...................................................................................................................................
Reason .....................................................................................................................................
...................................................................................................................................................
Sensor 2 ...................................................................................................................................
Reason .....................................................................................................................................
...............................................................................................................................................[4]
The intruder system is set up so that the alarm will only sound if two or more sensors have been
triggered.
An assembly language program has been written to process the contents of the memory location.
The table shows part of the instruction set for the processor used.
Instruction
Explanation
Op code Operand
Direct addressing. Load the contents of the given address to
LDD <address>
ACC
STO <address> Store the contents of ACC at the given address
INC <register> Add 1 to the contents of the register (ACC or IX)
ADD <address> Add the contents of the given address to the contents of ACC
Bitwise AND operation of the contents of ACC with the
AND <address>
contents of <address>
CMP #n Compare the contents of ACC with the number n
JMP <address> Jump to the given address
Following a compare instruction, jump to <address> if the
JPE <address>
compare was True
Following a compare instruction, jump to <address> if the
JGT <address> content of ACC is greater than the number used in the
compare instruction
END End the program and return to the operating system
Op code Operand
SENSORS: B00001010
COUNT: 0
VALUE: 1
LOOP: LDD SENSORS
AND VALUE
CMP #0
JPE ZERO
LDD COUNT
INC ACC
STO COUNT
ZERO: LDD VALUE
CMP #8
JPE EXIT
ADD VALUE
STO VALUE
JMP LOOP
EXIT: LDD COUNT
TEST: CMP …
JGT ALARM
(i) Dry run the assembly language code. Start at LOOP and finish when EXIT is reached.
[4]
.......................................................................................................................................[1]
(iii) The intruder detection system is improved and now has eight sensors.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [2]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [2]
6 A large warehouse stores goods that must be kept above a temperature of 15 degrees Celsius.
The warehouse has six temperature sensors which are each placed at a different location in the
warehouse.
A computer system is programmed to turn on appropriate heaters when one of the sensors is
below the minimum temperature.
(a) (i) State the name given to the type of system described.
...................................................................................................................................... [1]
...........................................................................................................................................
...................................................................................................................................... [1]
(b) Sensors and heaters are two types of device used in this system.
State two other devices that are used. Justify your choice.
Device 1 ....................................................................................................................................
Justification ...............................................................................................................................
...................................................................................................................................................
Device 2 ....................................................................................................................................
Justification ...............................................................................................................................
...................................................................................................................................................
[4]
(c) The computer system stores the temperature readings for the six sensors in six 8-bit memory
locations.
Six of the bits in an 8-bit register, LOWREG, are used to indicate whether a particular reading
is below the minimum temperature. A value of 1 means the reading is below the minimum
temperature.
For example:
This pattern of bits in LOWREG shows that sensor 5, sensor 4 and sensor 1 have readings
below the minimum temperature.
6 5 4 3 2 1
Not used Not used 0 1 1 0 0 1
The following table shows part of the instruction set for a processor which has one general
purpose register, the Accumulator (ACC), and an Index Register (IX).
Instruction
Explanation
Op code Operand
Direct addressing. Load the contents of the given address to
LDD <address>
ACC.
LDR #n Immediate addressing. Load the number n to IX.
Indexed addressing. Form the address from <address> +
LDX <address> the contents of the index register. Copy the contents of this
calculated address to ACC.
STO <address> Store the contents of ACC at the given address.
INC <register> Add 1 to the contents of the register (ACC or IX).
ADD <address> Add the contents of the given address to the ACC.
Bitwise OR operation of the contents of ACC with the contents
OR <address>
of address.
CMP #n Compare the contents of ACC with number n.
CMP <address> Compare the contents of ACC with the contents of <address>.
JMP <address> Jump to the given address.
Following a compare instruction, jump to <address> if the
JPE <address>
compare was True.
Following a compare instruction, jump to <address> if the
JGE <address> content of ACC is greater than or equal to the number used in
the compare instruction.
LOWTEMP: 15
LOWREG: B00000000
COUNTER: 1
START: LDR #0
CMP LOWTEMP
JGE TEMPOK
LDD LOWREG
OR COUNTER
STO LOWREG
JPE HEATON
ADD COUNTER
STO COUNTER
INC IX
JMP LOOP
(i) The code uses six memory locations to store the temperature readings. It stores readings
for sensors 1 to 6 at addresses 8000 to 8005.
Dry run the assembly language code starting at START and finishing when the loop has been
processed twice.
15 B00000000 1
[4]
6 The environment in a very large greenhouse is managed by a computer system. The system uses
a number of different sensors that include temperature sensors. In addition, the system controls a
number of heaters, windows and sprinklers.
(a) State one other type of sensor that could be used with this system.
Sensor ......................................................................................................................................
Justification ...............................................................................................................................
...................................................................................................................................................
[2]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[3]
(c) (i) The system makes use of a number of parameters. These parameters are used in the
code that runs the system.
State one of the parameters used in controlling the temperature in the greenhouse.
.......................................................................................................................................[1]
(ii) Explain how the parameter identified in part (c)(i) is used in the feedback process.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[2]
(d) There are eight temperature sensors numbered 1 to 8. Readings from these sensors are
stored in four 16-bit memory locations. The memory locations have addresses from 4000 to
4003. Each memory location stores two sensor readings as two unsigned binary integers.
Sensor 1 reading is stored in bits 8 to 15 of address 4000; Sensor 2 reading is stored in bits
0 to 7 of address 4000 and so on. The diagram shows that the current sensor 1 reading has
a value of 97.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
4000 0 1 1 0 0 0 0 1 0 0 1 1 1 0 0 1
4001 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0
4002 0 0 0 1 0 1 0 0 0 0 0 0 1 1 0 1
4003 1 0 0 0 0 0 1 0 1 1 0 0 0 1 0 1
(i) Give the denary value of the current reading for Sensor 5.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[1]
(ii) The following table shows part of the instruction set for a processor. The processor has
one general purpose register, the Accumulator (ACC).
Instruction
Explanation
Op code Operand
LDD <address> Direct addressing. Load the contents of the location at the given
address to ACC.
AND #n Bitwise AND operation of the contents of ACC with the operand.
AND <address> Bitwise AND operation of the contents of ACC with the contents of
<address>.
XOR #n Bitwise XOR operation of the contents of ACC with the operand.
XOR <address> Bitwise XOR operation of the contents of ACC with the contents of
<address>.
OR #n Bitwise OR operation of the contents of ACC with the operand.
OR <address> Bitwise OR operation of the contents of ACC with the contents of
<address>.
The reading for Sensor 5 is used in a calculation. The calculation is carried out by two
assembly language instructions.
The first instruction loads the contents of the 16-bit location that contains the value for
Sensor 5.
The second instruction moves the bits in Sensor 5 so that the 16-bit value is the value of
Sensor 5.
Complete the two instructions in the following code. Use the instruction set provided.
6 The compilation process has a number of stages. The first stage is lexical analysis.
A compiler uses a keyword table and a symbol table. Part of the keyword table is shown.
Keyword Token
01
* 02
= 03
IF 4A
THEN 4B
ENDIF 4C
ELSE 4D
FOR 4E
STEP 4F
TO 50
INPUT 51
OUTPUT 52
ENDFOR 53
Entries in the symbol table are allocated tokens. These values start from 60 (hexadecimal).
Start 1
INPUT Number
// Output values in a loop
FOR Counter Start TO 12
OUTPUT Number * Counter
ENDFOR
(a) Complete the symbol table to show its contents after the lexical analysis stage.
Token
Symbol
Value Type
Start 60 Variable
1 61 Constant
[3]
(b) The output from the lexical analysis stage is stored in the following table. Each cell stores one
byte of the output.
Complete the output from the lexical analysis stage. Use the keyword table and your answer
to part (a).
60 01
[2]
(c) The output of the lexical analysis stage is the input to the syntax analysis stage.
1 ................................................................................................................................................
...................................................................................................................................................
2 ................................................................................................................................................
...................................................................................................................................................
[2]
(i) Code optimisation produces code that minimises the amount of memory used.
...........................................................................................................................................
.......................................................................................................................................[1]
After the syntax analysis stage is complete, the compiler generates object code.
X A + B
Y A + B + C
Z A + B + C + D
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[5]
BLANK PAGE
4 A compiler uses a keyword table and a symbol table. Part of the keyword table is shown.
Keyword Token
01
+ 02
= 03
IF 4A
THEN 4B
ENDIF 4C
ELSE 4D
FOR 4E
STEP 4F
TO 50
INPUT 51
OUTPUT 52
ENDFOR 53
Entries in the symbol table are allocated tokens. These values start from 60 (hexadecimal).
INPUT Number1
INPUT Number2
INPUT Answer
IF Answer = Number1 + Number2
THEN
OUTPUT 10
ELSE
OUTPUT 0
ENDIF
(a) Complete the symbol table to show its contents after the lexical analysis stage.
Token
Symbol
Value Type
Number1 60 Variable
Number2 61 Variable
[3]
© UCLES 2018 9608/32/O/N/18
9
(b) The output from the lexical analysis stage is stored in the following table. Each cell stores one
byte of the output.
Complete the output from the lexical analysis. Use the keyword table and your answer to
part (a).
51 60
[2]
After the syntax analysis is complete, the compiler generates object code.
(i) Identify the final stage in the compilation process that follows this code generation stage.
.......................................................................................................................................[1]
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[3]
(iii) State two benefits of the process that is carried out in the final stage.
Benefit 1 ............................................................................................................................
...........................................................................................................................................
Benefit 2 ............................................................................................................................
...........................................................................................................................................
[2]
(d) An interpreter is executing a program. The program uses the variables a, b, c and d.
The program contains an expression that is written in infix form. The interpreter converts the
infix expression to RPN.
Show the changing contents of the stack as the interpreter evaluates the expression.
The first entry on the stack has been done for you.
2
[4]
5 (a) Most desktop or laptop computers use CISC (Complex Instruction Set Computing)
architecture. Most smartphones and tablets use RISC (Reduced Instruction Set Computing).
State four features that are different for the CISC and RISC architectures.
1 ................................................................................................................................................
...................................................................................................................................................
2 ................................................................................................................................................
...................................................................................................................................................
3 ................................................................................................................................................
...................................................................................................................................................
4 ................................................................................................................................................
...................................................................................................................................................
[4]
4 A compiler uses a keyword table and a symbol table. Part of the keyword table is shown.
Keyword Token
01
+ 02
= 03
<> 04
IF 4A
THEN 4B
ENDIF 4C
ELSE 4D
REPEAT 4E
UNTIL 4F
TO 50
INPUT 51
OUTPUT 52
ENDFOR 53
Entries in the symbol table are allocated tokens. These values start from 60 (hexadecimal).
Counter 0
INPUT Password
REPEAT
IF Password <> "Cambridge"
THEN
INPUT Password
ENDIF
Counter Counter + 1
UNTIL Password = "Cambridge"
OUTPUT Counter
(a) Complete the symbol table to show its contents after the lexical analysis stage.
Token
Symbol
Value Type
Counter 60 Variable
[3]
(b) The output from the lexical analysis stage is stored in the following table. Each cell stores one
byte of the output.
Complete the output from the lexical analysis using the keyword table and your answer to
part (a).
60 01
[2]
(c) The following table shows assembly language instructions for a processor which has one
general purpose register, the Accumulator (ACC).
Instruction
Explanation
Op code Operand
Direct addressing. Load the contents of the location at the given
LDD <address>
address to ACC
ADD <address> Add the contents of the given address to the ACC
STO <address> Store the contents of ACC at the given address
After the syntax analysis is completed successfully, the compiler generates object code.
X = X + Y
Z = Z + X
LDD 236
ADD 237
STO 236
LDD 238
ADD 236
STO 238
(i) The final stage in the compilation process that follows this code generation stage is code
optimisation.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [3]
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [2]