Processes and Two's complement Arithmetic

Class: CSCE-313


Notes:

Two's Complement (8-bit Arithmetic)

Consider 2's complement representation of 8-bit numbers ranging from -128 ... +127. Answer the following questions:


Question 1

Given a positive number 0<u<128, what is the decimal value of the unsigned number that represents u.

Options:

Overall explanation:

Tags: Bits and Bytes#Mapping Signed <-> Unsigned

Question 2

Show that (u)=u in 2's complement arithmetic.

Answer:
The two's complement of a number u for an n-bit system is defined as:

u=2nu

To get to (u) we need to negate again:

(u)=2n(2nu)

Pushing the negative inside and simplifying:

(u)=2n2n+u=u

Question 3

What is the negation of -128 in 8-bit 2's complement? Write a valid number in 8-bit 2's complement.

Let's apply the "invert and add 1" rule to -128:

  1. Binary for -128: 1000 0000
  2. Invert the bits: 0111 1111
  3. Add 1: 1000 0000

The result is exactly what you started with: 1000 0000, which represents -128. This is known as an integer overflow.

Since the range is [128,127], any integer within that inclusive span is a valid number. Here are a few examples in both decimal and binary:

Decimal 8-bit Binary Note
0 0000 0000 The additive identity
127 0111 1111 The maximum positive value (TMax)
-1 1111 1111 All bits set
-128 1000 0000 The most negative value (TMin)
42 0010 1010 A standard positive integer

−128 is the only number in two’s complement whose negation equals itself due to overflow.

Answer: 100000

Question 4

Which operation is used to compute the two’s complement of a number?

Options:

Overall explanation:

Tags: Bits and Bytes


Two’s Complement Conversion

Convert the following decimal numbers to 8-bit 2's complement representation


Question 5

+42 =

  1. Binary for +42: 0010 1010
    • For converting to binary: 4210=32+8+2
    • Since it's positive, no change needed.
    • Answer: 0010 1010

Question 6

-17 =

  1. Binary for -17: 0001 0001
    • Note: 1710=16+1
  2. Invert the bits: 1110 1110
  3. Add 1: 1110 1111
    • Answer: 1110 1111

Overflow Conditions in Two’s Complement Addition

An overflow in 2's complement arithmetic means that the true result cannot be represented in the precision of the arithmetic. Note that this is not the same as carry resulting from the arithmetic. Although I didn't cover overflow in signed arithmetic in class, this is an opportunity to read up on it and understand the difference between overflow and carry in 2's complement arithmetic. Consider the 8-bit wheel in your class slides that shows all signed numbers representable in 8 bits.


Question 7

In 2's complement arithmetic, if you add two positive numbers and get a negative result, has overflow definitely occurred?

Options:

Overall explanation:

The Rules of Overflow
You can detect overflow easily by looking at the sign bits of the operands and the result:

Operation Operand A Sign Operand B Sign Result Sign Overflow?
A + B Positive (+) Positive (+) Negative (-) YES
A + B Negative (-) Negative (-) Positive (+) YES
A + B Positive (+) Negative (-) Either NO (Never)

Note: Overflow can never occur when adding a positive number and a negative number, because the sum will always be somewhere between the two original values, staying within the representable range.

Tags: Bits and Bytes

Question 8

If you add two negative numbers and get a positive result, has overflow definitely occurred.

Options:

Overall explanation:

Tags: Bits and Bytes

Question 9

You cannot have an overflow when you add a positive number to a negative number.

Options:

Overall explanation:

Tags: Bits and Bytes


Processes & Interrupts

A process is an instance of a running program


Question 10

A user-level library implements each system call by executing a “transition to kernel mode” instruction followed by a procedure call to an appropriate system call handler in the kernel. To make this work, the user-level library must be built with access to a symbol table that contains the kernel addresses of each system call.

Options:

Overall explanation:

How the Process Actually Works

Step Location Action
1. Wrapper Call User Space You call printf(), which calls the library wrapper write().
2. Preparation User Space The library puts the arguments and the System Call Number into registers.
3. Trap Hardware The library executes the syscall instruction. The CPU switches to Kernel Mode.
4. Dispatch Kernel Space The kernel looks at the number in the register and indexes its own internal dispatch table to find the handler.
5. Return Kernel Space The kernel finishes, puts the result in a register, and executes iret or sysret to go back to User Mode.

Tags: 02 - Architectural Support for Operating Systems#System calls example

Question 11

Describe what happens when an external hardware driven interrupt occurs?

  1. The Interrupt Signal
    • The hardware device sends an electrical signal to a specialized chip called the Interrupt Controller (e.g., the APIC in modern systems). If the interrupt is not "masked" (ignored), the controller signals the CPU's interrupt pin.
  2. Immediate Hardware Response
    • The CPU finishes the execution of the current instruction. Before starting the next one, it checks for pending interrupts. If one exists:
      • Mode Switch: The CPU automatically switches from User Mode to Kernel Mode.
      • State Saving: The hardware saves the current Program Counter (PC) and the Processor Status Word (PSW) onto the kernel stack so the CPU can eventually return to exactly where it left off.
  3. The Interrupt Vector Table (IVT)
    • The CPU uses an Interrupt Vector (an index number) provided by the hardware to look up an address in the Interrupt Vector Table (or Interrupt Descriptor Table). This table contains the memory addresses of the various Interrupt Service Routines (ISRs).
  4. Executing the ISR (The Handler)
    • The CPU jumps to the address found in the table and begins executing the Interrupt Service Routine.
      • Software State Saving: The ISR usually saves the rest of the registers (like %rax, %rbx, etc.) that the hardware didn't automatically save.
      • Servicing: The code interacts with the hardware device (e.g., moving data from the keyboard buffer into memory).
  5. Resuming the Interrupted Process
    • Once the handler is finished:
      1. The ISR restores the saved registers from the stack.
      2. It executes a special instruction (like iret or "Interrupt Return").
      3. Restoration: This instruction pops the saved PSW and Program Counter back into the CPU registers.
      4. Mode Switch: The CPU switches back to User Mode and continues the original program as if nothing had happened.

Answer:
An external hardware interrupt occurs when a device signals the interrupt controller, which supplies an interrupt vector to the CPU. The CPU completes the current instruction, switches to kernel mode, saves its state, looks up the handler address in the interrupt vector table, and executes the corresponding interrupt service routine. After servicing the interrupt, the CPU restores its state and resumes execution.

Steps:

Question 12

Which of the following best characterizes a process?

Options:

Overall explanation:

Tags: 02 - Architectural Support for Operating Systems

Question 13

Briefly explain why a single-core CPU can appear to run many processes simultaneously.

A single-core CPU creates the illusion of simultaneity through a technique called Context Switching (often referred to as Time Slicing).

Even though a single core can only execute one instruction at a time, it operates at incredibly high speeds—billions of cycles per second. The Operating System (OS) leverages this speed to manage multiple processes using the following mechanism:

This management is handled by the Scheduler, which is a core component of the OS kernel responsible for deciding which process gets the CPU next.

Answer:
A single-core CPU can look like it's running many processes at once because the OS quickly switches between them, this is called context switching and enables each process to get a small time slice on the CPU. The CPU is running and switching between processes so fast that, to any user, it appears that the computer is running all the programs simultaneously.

Tags: 01 - OS, Process, Multitasking, and Virtual Memory#Multitasking

Question 14

Each process has its own virtual address space. Which of the following statement(s) is/are true?

Options:

Overall explanation:

Tags: 01 - OS, Process, Multitasking, and Virtual Memory#Virtual Memory


x86-64 Privilege Levels and Protection

The x86-64 architecture supports multiple privilege levels (rings).


Question 15

Which ring is used for user programs?

Options:

Overall explanation:

Tags: 02 - Architectural Support for Operating Systems#1. Privilege levels differentiate instruction sets

Question 16

Why are privileged instructions restricted to the kernel?

Options:

Overall explanation:

Tags: 02 - Architectural Support for Operating Systems

Question 17

When a user program executes a system call on x86-64 Linux: Which of the following sequences is correct? On x86-64 with the syscall instruction, the processor does a fast transition into the kernel.

On entry (syscall), user places:

On return (sysret), kernel sets:

Options:

Overall explanation:

Summary:

Purpose Register
Syscall Number %rax
Arguments (1-6) %rdi, %rsi, %rdx, %r10, %r8, %r9
Return Address %rcx (Saved by hardware)
Saved Flags %r11 (Saved by hardware)
Return Value %rax

Tags: 02 - Architectural Support for Operating Systems#System calls an example in assembly