13.5 Assembly Language

COS2621 - Computer Organisation · 13. Instruction Sets: Addressing Modes and Formats

Assembly Language

Introduction to Assembly Language

Assembly language is a low-level programming language closely related to machine code. Each assembly instruction corresponds directly to a machine instruction specific to the architecture of the computer. This language allows for precise control over computer operations, making it suitable for programming hardware and system software.

Basic Structure of Assembly Language

An assembly language program consists of instructions that the CPU can execute. Each instruction typically has three parts: the operation code (opcode), the operands, and optional labels. For example, the instruction MOV AX, 5 moves the value 5 into the AX register. The opcode specifies the operation, while the operands specify the data or addresses involved.

Registers in Assembly Language

Registers are small storage locations within the CPU used to hold data temporarily. Common registers in x86 architecture include:

  • AX: Accumulator register, used for arithmetic operations.
  • BX: Base register, often used for addressing.
  • CX: Counter register, used in loop operations.
  • DX: Data register, used for I/O operations.

Example of a Simple Assembly Program

Consider a simple assembly program that adds two numbers:

section .data
num1 db 10
num2 db 20
result db 0
section .text
global _start
_start:
    mov al, [num1] ; Load num1 into AL
    add al, [num2] ; Add num2 to AL
    mov [result], al ; Store the result
    ; Exit program
    mov eax, 1 ; syscall: exit
    xor ebx, ebx ; status: 0
    int 0x80 ; call kernel

This program defines two numbers, adds them, and stores the result. The MOV instruction is used to transfer data between registers and memory.

Using NASM for Assembly Language Programming

The Netwide Assembler (NASM) is a popular assembler for writing x86 assembly language programs. To use NASM:

  1. Write your program in a text editor and save it with a .asm extension.
  2. Assemble the program using the command: nasm -f elf64 your_program.asm.
  3. Link the program using: ld -o your_program your_program.o.
  4. Run the program with: ./your_program.

Tip: Make sure to check for syntax errors in your code by reviewing the NASM error messages.

Common Instructions in Assembly Language

Common assembly instructions include:

  • TEST: Tests individual bits in the destination operand against those in the source operand. This instruction can be used to set flags without modifying the operands.

Watch out: Ensure that you correctly specify data sizes. Mismatched sizes can lead to unexpected results.

Debugging Assembly Programs

Debugging is crucial in assembly programming. Use the DEBUG utility to step through your program. This tool allows you to:

  • Examine the contents of registers.
  • View memory locations.
  • Step through instructions one at a time.

Example Questions

Consider the following assembly instruction: mov ax, 0024. Which number will be stored in the AX register if we assemble it using (i) NASM, and (ii) DEBUG?

  • (i) AX = (0024)16
  • (ii) AX = (0024)16

Which one of the following instructions would you use if you want to branch to the label TEST if the sign flag is set?

  • JS TEST

Check your understanding

  • What does the TEST instruction do?
  • Write a simple assembly instruction to add two numbers and store the result.
  • What is the purpose of the RET instruction in assembly language?

Test yourself

5 quick questions on this lesson. Pick an answer to see if you're right.

  1. 1. What does the LOOP instruction do in assembly language?

  2. 2. What does the STC instruction do?

  3. 3. NASM is a(n) ___________ while DEBUG is a(n) _______.

  4. 4. What is the function of the TEST instruction in assembly language?

  5. 5. If we want to read a string of characters from the keyboard using an int 21h instruction, the AH register should be set to __________