Instruction Codes, Implicit Threading and Middleware for Mobile Computing

Three syllabus items in Computer Science and Applications were taught only inside borrowed GATE chapters or in a line: stored-program organisation and instruction codes (Unit 2), implicit threading (Unit 5) and middleware and gateways for mobile computing (Unit 9). This chapter teaches each, with a worked program on the basic computer, instruction-format counts, Amdahl's law and the problem-and-response logic of mobile middleware.
Overview

1. Stored-program organisation and instruction codes in the basic computer

2 min read · 2 parts

In a stored-program computer the instructions and the data live in the same memory, and the control unit fetches the instruction at one address, interprets it and carries it out, then moves on. An instruction code is the group of bits that tells the computer what to do; it has at least an operation code (opcode), which names the operation, and, for most instructions, an operand field that gives an address. The textbook 'basic computer' (Mano) has a memory of 4,096 words of 16 bits, so an address needs 12 bits and a 16-bit instruction is split as follows: bit 15 is the mode bit I (0 for direct addressing, 1 for indirect), bits 14 to 12 are a 3-bit opcode, and bits 11 to 0 are the address. With a direct address the operand is at that location; with an indirect address, that location holds the operand's address.

Opcodes 000 to 110 are the seven memory-reference instructions: AND (000), ADD (001), LDA (load the accumulator, 010), STA (store the accumulator, 011), BUN (branch unconditionally, 100), BSA (branch and save the return address, 101) and ISZ (increment and skip if zero, 110). Opcode 111 with I = 0 selects a register-reference instruction, in which the lower 12 bits pick an operation on the registers (for example 7800 is CLA, clear the accumulator, and 7001 is HLT), and opcode 111 with I = 1 selects an input-output instruction (F800 is INP and F400 is OUT). A short program shows the idea. Put LDA 200 (2200), ADD 201 (1201), STA 202 (3202) and HLT (7001) at addresses 100 to 103, and the numbers 5 and 7 at 200 and 201. The accumulator becomes 5, then 12, and 12 is stored at 202, where the program ends. With the indirect bit set, ADD 201 would be written 9201 instead of 1201.

Instruction format of the basic computer (16 bits)
BitsFieldMeaning
15I0 direct, 1 indirect
14 to 12Opcode000 to 110 memory-reference; 111 register or I/O
11 to 0AddressOne of 4,096 words
⚠️ The trap: the first byte of a hexadecimal code
In hexadecimal the leading digit is I followed by the opcode: 2200 starts with 0010, so I = 0 and opcode 010, which is LDA; 9201 starts with 1001, so I = 1 and opcode 001, which is ADD. A candidate who reads the digit '9' as an opcode of nine has forgotten that the top bit is the mode bit, not part of the opcode.

In a stored-program computer the instructions and the data live in the same memory, and the control unit fetches the instruction at one address, interprets it and carries it out, then moves…

2. How many addresses an instruction names: zero, one, two and three-address codes

1 min read · 1 part

The number of address fields in an instruction is a design choice. A three-address instruction names two sources and a destination (ADD R1, A, B), a two-address instruction overwrites one source with the result (ADD R1, B), a one-address instruction uses an implicit accumulator (ADD B), and a zero-address instruction takes its operands from the top of a stack (ADD). More addresses per instruction means fewer instructions per program, but longer instructions and more memory references. The standard exercise computes X = (A + B) × (C + D), and the counts differ.

X = (A + B) × (C + D) in each format
FormatInstructionsCount
Three-addressADD R1, A, B; ADD R2, C, D; MUL X, R1, R23
Two-addressMOV R1, A; ADD R1, B; MOV R2, C; ADD R2, D; MUL R1, R2; MOV X, R16
One-addressLOAD A; ADD B; STORE T; LOAD C; ADD D; MUL T; STORE X7
Zero-address (stack)PUSH A; PUSH B; ADD; PUSH C; PUSH D; ADD; MUL; POP X8

Zero-address code is used by stack machines and is written from the postfix form of the expression: (A + B) × (C + D) becomes A B + C D + ×, and each operand is pushed, each operator pops its two operands and pushes the result. Two further ideas appear in the unit: an addressing mode says how the address field is turned into an operand address (immediate, direct, indirect, register, register indirect, indexed, base-register and relative), and instruction length can be fixed (as in RISC) or variable (as in CISC).

The number of address fields in an instruction is a design choice. A three-address instruction names two sources and a destination (ADD R1, A, B), a two-address instruction overwrites one…

3. Implicit threading: letting the compiler and runtime create the threads

2 min read · 1 part

With explicit threading the programmer creates and manages threads. As the number of cores grows this becomes error-prone, so implicit threading hands the creation and management of threads to compilers and run-time libraries: the programmer identifies tasks that can run in parallel, not threads. Four approaches are standard. A thread pool creates a number of threads at start-up and gives them work from a queue, which avoids the cost of creating a thread for each request and bounds the number of threads. Fork-join splits a task into subtasks (fork) and waits for them (join), as in Java's fork-join framework, which uses work-stealing so idle threads take tasks from busy ones. OpenMP is a set of compiler directives and a library: a line such as #pragma omp parallel for before a C loop tells the compiler to divide the iterations among threads. Grand Central Dispatch (Apple) places blocks of code in dispatch queues, serial or concurrent, from which a pool of threads takes them.

The limit on all this is Amdahl's law: if a fraction S of a program must run serially, the speed-up on N processors is at most 1/(S + (1 − S)/N). With S = 0.25 the speed-up on 4 cores is 1/(0.25 + 0.1875) = 2.29, on 8 cores 2.91, and no number of cores can take it past 1/S = 4. The syllabus pairs this with threading issues: the semantics of fork() and exec() in a multithreaded process, signal handling (to which thread a signal is delivered), thread cancellation (asynchronous, which ends the thread at once, or deferred, which lets it check at safe points), thread-local storage, and scheduler activations, the kernel-to-library upcalls that keep the number of kernel threads right in a many-to-many model.

Four implicit-threading approaches
ApproachIdeaExample
Thread poolPre-created threads take work from a queueJava executors, Windows thread pool
Fork-joinSplit a task, then wait for the partsJava ForkJoinPool (work stealing)
OpenMPCompiler directives mark parallel regions#pragma omp parallel for
Grand Central DispatchBlocks in serial or concurrent queuesApple macOS and iOS

With explicit threading the programmer creates and manages threads. As the number of cores grows this becomes error-prone, so implicit threading hands the creation and management of threads…

4. Middleware and gateways for mobile computing

2 min read · 3 parts

Middleware is software between the operating system and the applications that hides differences of hardware, network and operating system and gives programmers a common set of services. A mobile device raises problems an office computer does not: it is disconnected or weakly connected much of the time, its bandwidth is low and varies, its battery is small, its location changes and the platforms are heterogeneous. Mobile middleware is built around these facts. Message-oriented middleware queues messages so a sender and a receiver need not be connected at the same time; tuple-space middleware (as in Lime) lets devices share data through a common space they can read and write without knowing each other; context-aware middleware adapts to location, bandwidth and battery; transactional and synchronising middleware supports disconnected work, so that the user edits a local copy and the system reconciles it later (the idea in Coda and Bayou); and mobile-agent middleware sends code to the data rather than data to the device.

A gateway connects two networks that use different protocols and translates between them. The classic mobile example is the WAP gateway: in the first WAP architecture a phone spoke a lightweight protocol stack (WAP, with pages written in WML) over the wireless link to the gateway, which converted it to ordinary HTTP and HTML requests to a web server and compressed the reply, so a thin device could browse. Later versions moved to standard internet protocols and the gateway became a proxy. An SMS gateway is another example, passing text between a web application and the cellular network. Mobile IP is the network-layer partner of this middleware: the device keeps a permanent home address with a home agent, takes a temporary care-of address through a foreign agent in the visited network, and the home agent tunnels packets to it.

Mobile middleware: the problem and the response
ProblemMiddleware response
Frequent disconnectionMessage queues; local copies reconciled later
Low or varying bandwidthCompression, caching, gateways
Changing location and contextContext-aware adaptation
Heterogeneous platformsA common service layer; gateways translating protocols
The first WAP gateway
  1. Phone speaks WAP; pages in WMLOver the wireless link
  2. WAP gateway converts to HTTP and HTML
  3. Web server answers
  4. Gateway compresses the replySo a thin device can browse

Middleware is software between the operating system and the applications that hides differences of hardware, network and operating system and gives programmers a common set of services. A…

Key takeaways

  • The basic computer's 16-bit instruction is a mode bit I, a 3-bit opcode and a 12-bit address; opcodes 000 to 110 are memory-reference instructions and 111 selects register-reference (I = 0) or input-output (I = 1).
  • X = (A + B) × (C + D) takes 3, 6, 7 and 8 instructions in three-, two-, one- and zero-address formats.
  • Implicit threading gives thread creation to compilers and libraries through thread pools, fork-join, OpenMP and Grand Central Dispatch.
  • Amdahl's law limits speed-up to 1/(S + (1 − S)/N): with a quarter of the program serial the speed-up is 2.29 on 4 cores and never exceeds 4.
  • Mobile middleware answers disconnection, low bandwidth, changing context and heterogeneity with queues, synchronisation, adaptation and gateways; a WAP gateway translates between the wireless protocol stack and HTTP.

Practice questions (24)

Attempt each one before opening the answer. Every explanation names the tempting wrong option as well as the right one, because that is where marks are lost.

  1. In the basic computer, how many bits does a memory address need, given 4,096 words?

    1. 8
    2. 12
    3. 16
    4. 10
    Show answer

    Answer: B — 12

    4,096 = 2 to the power 12, so a 12-bit address names every word. The 16-bit instruction then has 3 bits of opcode and one mode bit.
  2. The instruction 9201 in the basic computer is:

    1. LDA with direct addressing
    2. STA with indirect addressing
    3. ADD with indirect addressing
    4. BUN with direct addressing
    Show answer

    Answer: C — ADD with indirect addressing

    The leading hex digit 9 is 1001: I = 1 for indirect and opcode 001 for ADD. LDA is 010 and STA is 011.
  3. Starting with M[200] = 5 and M[201] = 7, the program LDA 200, ADD 201, STA 202, HLT leaves in location 202:

    1. 5
    2. 7
    3. 35
    4. 12
    Show answer

    Answer: D — 12

    LDA puts 5 in the accumulator, ADD makes 5 + 7 = 12, and STA writes 12 to 202. The number 35 is a product, which no instruction here computes.
Show the other 21 questions
  1. The operation code 111 with the mode bit I = 1 selects an:

    1. Input-output instruction
    2. Register-reference instruction
    3. Memory-reference instruction
    4. Interrupt return only
    Show answer

    Answer: A — Input-output instruction

    With I = 0 the code 111 is register-reference, as in 7800 (CLA). With I = 1 it is input-output, as in F800 (INP) and F400 (OUT).
  2. To evaluate X = (A + B) × (C + D), a zero-address (stack) machine needs how many instructions?

    1. 3
    2. 8
    3. 6
    4. 7
    Show answer

    Answer: B — 8

    PUSH A, PUSH B, ADD, PUSH C, PUSH D, ADD, MUL, POP X: eight. Three is the count for a three-address machine and seven for one-address code.
  3. In implicit threading, the programmer identifies:

    1. Kernel threads to create
    2. Hardware registers to use
    3. Tasks that can run in parallel
    4. The scheduler's time slice
    Show answer

    Answer: C — Tasks that can run in parallel

    The compiler or run-time library maps the tasks to threads. With explicit threading the programmer would create and manage the threads.
  4. The line '#pragma omp parallel for' before a C loop is an example of:

    1. A Java fork-join task
    2. A thread pool created at start-up
    3. A Grand Central Dispatch queue
    4. OpenMP, a compiler-directive approach to implicit threading
    Show answer

    Answer: D — OpenMP, a compiler-directive approach to implicit threading

    OpenMP marks parallel regions with compiler directives and the run-time divides the iterations among threads. The other items use libraries or language runtimes.
  5. If 25 per cent of a program must run serially, the maximum possible speed-up on any number of cores, by Amdahl's law, is:

    1. 4
    2. 2.29
    3. 8
    4. 25
    Show answer

    Answer: A — 4

    The limit is 1/S = 1/0.25 = 4. The 2.29 is the speed-up on 4 cores, not the limit.
  6. Which problem is the main reason for message-queuing middleware in mobile computing?

    1. The screen is too small
    2. The sender and receiver are often not connected at the same time
    3. Processors are too fast
    4. Memory is unlimited
    Show answer

    Answer: B — The sender and receiver are often not connected at the same time

    A queue stores a message until the receiver is reachable, which handles disconnection. The other options are not the issue it addresses.
  7. In first-generation WAP, the gateway's main job was to:

    1. Route packets between two Ethernet segments
    2. Assign IP addresses to phones
    3. Translate between the WAP protocol stack and HTTP and compress the reply
    4. Encrypt voice calls end to end
    Show answer

    Answer: C — Translate between the WAP protocol stack and HTTP and compress the reply

    The phone spoke WAP with WML pages over the wireless link and the gateway made ordinary HTTP requests on its behalf. It was a protocol translator, not a router or address server.
  8. An instruction code in a computer is mainly a group of bits that:

    1. Stores the program name
    2. Holds only data
    3. Encrypts memory
    4. Instructs the computer to perform a specific operation
    Show answer

    Answer: D — Instructs the computer to perform a specific operation

    Instruction codes specify operations and operands. Holding only data tempts, but data bits are separate from the instruction format.
  9. In immediate addressing, the operand is:

    1. Contained in the instruction itself
    2. At a memory address given in the instruction
    3. In a register named by the instruction
    4. On the stack top
    Show answer

    Answer: A — Contained in the instruction itself

    The operand value is part of the instruction. A memory address tempts, but that is direct addressing.
  10. A stack-organised (zero-address) computer uses which pair of instructions to move data between memory and the stack?

    1. LOAD and STORE
    2. PUSH and POP
    3. MOV and JMP
    4. CALL and RET
    Show answer

    Answer: B — PUSH and POP

    PUSH places a value on the stack and POP removes it. LOAD and STORE tempt as memory moves, but they belong to register or accumulator machines.
  11. OpenMP and Intel Threading Building Blocks are examples of:

    1. Compilers for assembly
    2. Network protocols
    3. Implicit threading libraries
    4. Database engines
    Show answer

    Answer: C — Implicit threading libraries

    They let the compiler or runtime manage threads. Compilers for assembly tempt only because OpenMP uses compiler directives.
  12. A thread pool improves performance mainly by:

    1. Creating unlimited threads
    2. Disabling the scheduler
    3. Using only one thread
    4. Reusing a fixed set of pre-created threads instead of creating one per task
    Show answer

    Answer: D — Reusing a fixed set of pre-created threads instead of creating one per task

    It avoids repeated creation cost and bounds concurrency. Unlimited threads tempt as more parallelism, but they exhaust resources.
  13. For Y = A × B + C, the number of instructions in a three-address machine program (MUL T, A, B; ADD Y, T, C) is:

    1. 2
    2. 3
    3. 4
    4. 6
    Show answer

    Answer: A — 2

    Each three-address instruction names both sources and the destination, so two suffice. Four tempts as the accumulator count.
  14. For Y = A × B + C, the number of instructions on a single-accumulator (one-address) machine (LOAD A, MUL B, ADD C, STORE Y) is:

    1. 2
    2. 4
    3. 6
    4. 3
    Show answer

    Answer: B — 4

    Load, multiply, add and store make 4. Two tempts as the three-address count.
  15. A 16-bit instruction format uses 4 bits for the opcode and the rest for the address. The number of directly addressable memory words is:

    1. 65,536
    2. 16
    3. 4096
    4. 1024
    Show answer

    Answer: C — 4096

    12 address bits give 2¹² = 4096 words. The 65,536 tempts as 2¹⁶, ignoring the opcode bits.
  16. In indexed addressing with address field 1000 and an index register containing 25, the effective address is:

    1. 975
    2. 25
    3. 1000
    4. 1025
    Show answer

    Answer: D — 1025

    Effective address = address field + index = 1025. The 1000 tempts as the unmodified direct address.
  17. Message-queuing middleware is particularly suited to mobile computing because it:

    1. Stores messages until a disconnected device reconnects
    2. Requires a constant connection
    3. Prevents any communication
    4. Works only offline
    Show answer

    Answer: A — Stores messages until a disconnected device reconnects

    Queues decouple sender and receiver in time. Requiring constant connection tempts, but that describes synchronous RPC.
  18. To compute Y = A × B + C on a zero-address stack machine, the number of instructions (PUSH A, PUSH B, MUL, PUSH C, ADD, POP Y) is:

    1. 5
    2. 6
    3. 4
    4. 3
    Show answer

    Answer: B — 6

    Three pushes, two arithmetic operations and one pop make 6. Five tempts by leaving out the final POP to memory.
  19. An instruction set has 32 registers, a three-register format, a 6-bit opcode and 32-bit instructions. The number of unused bits per instruction is:

    1. 16
    2. 21
    3. 11
    4. 0
    Show answer

    Answer: C — 11

    Each register needs 5 bits, so 15 + 6 = 21 are used and 32 − 21 = 11 remain. The 21 tempts as the used count.
  20. A loop is 80% parallelisable with an OpenMP pragma and runs on 8 threads. By Amdahl's law the speed-up is approximately:

    1. 6.4
    2. 8
    3. 5
    4. 3.33
    Show answer

    Answer: D — 3.33

    Speed-up = 1/(0.2 + 0.8/8) = 1/0.3 ≈ 3.33. The 6.4 tempts as 0.8 × 8, ignoring the serial part.
  21. Given below are two statements. Statement I: In implicit threading, the programmer need not explicitly create or manage threads. Statement II: Implicit threading removes the possibility of race conditions on shared data. In the light of the above statements, choose the correct answer.

    1. Both Statement I and Statement II are true
    2. Both Statement I and Statement II are false
    3. Statement I is true, but Statement II is false
    4. Statement I is false, but Statement II is true
    Show answer

    Answer: C — Statement I is true, but Statement II is false

    Statement I is true: the compiler or runtime handles thread creation. Statement II is false: races on shared variables can still occur, so the programmer must still identify and guard them. Believing the runtime removes synchronisation problems is the tempting error.