Instruction Codes, Implicit Threading and Middleware for Mobile Computing
2. How many addresses an instruction names: zero, one, two and three-address codes
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.
| Format | Instructions | Count |
|---|---|---|
| Three-address | ADD R1, A, B; ADD R2, C, D; MUL X, R1, R2 | 3 |
| Two-address | MOV R1, A; ADD R1, B; MOV R2, C; ADD R2, D; MUL R1, R2; MOV X, R1 | 6 |
| One-address | LOAD A; ADD B; STORE T; LOAD C; ADD D; MUL T; STORE X | 7 |
| Zero-address (stack) | PUSH A; PUSH B; ADD; PUSH C; PUSH D; ADD; MUL; POP X | 8 |
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
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.
| Approach | Idea | Example |
|---|---|---|
| Thread pool | Pre-created threads take work from a queue | Java executors, Windows thread pool |
| Fork-join | Split a task, then wait for the parts | Java ForkJoinPool (work stealing) |
| OpenMP | Compiler directives mark parallel regions | #pragma omp parallel for |
| Grand Central Dispatch | Blocks in serial or concurrent queues | Apple 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
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.
| Problem | Middleware response |
|---|---|
| Frequent disconnection | Message queues; local copies reconciled later |
| Low or varying bandwidth | Compression, caching, gateways |
| Changing location and context | Context-aware adaptation |
| Heterogeneous platforms | A common service layer; gateways translating protocols |
- Phone speaks WAP; pages in WMLOver the wireless link
- WAP gateway converts to HTTP and HTML
- Web server answers
- 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.
In the basic computer, how many bits does a memory address need, given 4,096 words?
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.The instruction 9201 in the basic computer is:
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.Starting with M[200] = 5 and M[201] = 7, the program LDA 200, ADD 201, STA 202, HLT leaves in location 202:
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
The operation code 111 with the mode bit I = 1 selects an:
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).To evaluate X = (A + B) × (C + D), a zero-address (stack) machine needs how many instructions?
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.In implicit threading, the programmer identifies:
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.The line '#pragma omp parallel for' before a C loop is an example of:
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.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:
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.Which problem is the main reason for message-queuing middleware in mobile computing?
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.In first-generation WAP, the gateway's main job was to:
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.An instruction code in a computer is mainly a group of bits that:
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.In immediate addressing, the operand is:
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.A stack-organised (zero-address) computer uses which pair of instructions to move data between memory and the stack?
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.OpenMP and Intel Threading Building Blocks are examples of:
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.A thread pool improves performance mainly by:
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.For Y = A × B + C, the number of instructions in a three-address machine program (MUL T, A, B; ADD Y, T, C) is:
Show answer
Answer: A — 2
Each three-address instruction names both sources and the destination, so two suffice. Four tempts as the accumulator count.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:
Show answer
Answer: B — 4
Load, multiply, add and store make 4. Two tempts as the three-address count.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:
Show answer
Answer: C — 4096
12 address bits give 2¹² = 4096 words. The 65,536 tempts as 2¹⁶, ignoring the opcode bits.In indexed addressing with address field 1000 and an index register containing 25, the effective address is:
Show answer
Answer: D — 1025
Effective address = address field + index = 1025. The 1000 tempts as the unmodified direct address.Message-queuing middleware is particularly suited to mobile computing because it:
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.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:
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.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:
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.A loop is 80% parallelisable with an OpenMP pragma and runs on 8 threads. By Amdahl's law the speed-up is approximately:
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.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.
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.