Introduction To Assembly Language Programming: Computer Organization Prof. Muhamed Mudawar
Introduction To Assembly Language Programming: Computer Organization Prof. Muhamed Mudawar
Language Programming
COE 301
Computer Organization
Prof. Muhamed Mudawar
College of Computer Sciences and Engineering
King Fahd University of Petroleum and Minerals
Next . . .
System Calls
Defining Data
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 2
Instruction Set Architecture (ISA)
Critical Interface between software and hardware
An ISA includes the following …
Instructions and Instruction Formats
Data Types, Encodings, and Representations
Programmable Storage: Registers and Memory
Addressing Modes: to address Instructions and Data
Handling Exceptional Conditions (like overflow)
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 5
MIPS General-Purpose Registers
32 General Purpose Registers (GPRs)
All registers are 32-bit wide in the MIPS 32-bit architecture
Software defines names for registers to standardize their use
Assembler can refer to registers by name or by number ($ notation)
Immediate (I-Type)
16-bit immediate constant is part in the instruction
Jump (J-Type)
Used by jump instructions
Op6 immediate26
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 7
Next . . .
System Calls
Defining Data
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 8
What is Assembly Language?
Low-level programming language for a computer
1. Executable Instructions
Generate machine code for the processor to execute at runtime
Instructions tell the processor what to do
3. Assembler Directives
Provide information to the assembler while translating a program
Used to define segments, allocate memory variables, etc.
Non-executable: directives are not part of the instruction set
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 10
Assembly Language Instructions
Assembly language instructions have the format:
[label:] mnemonic [operands] [#comment]
Label: (optional)
Marks the address of a memory location, must have a colon
Typically appear in data and text segments
Mnemonic
Identifies the operation (e.g. add, sub, etc.)
Operands
Specify the data required by the operation
Operands can be registers, memory variables, or constants
Most instructions have three operands
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 12
Program Template
# Title: Filename:
# Author:Date:
# Description:
# Input:
# Output:
################# Data segment #####################
.data
. . .
################# Code segment #####################
.text
.globl main
main: # main program entry
. . .
li $v0, 10 # Exit program
syscall
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 13
.DATA, .TEXT, & .GLOBL Directives
.DATA directive
Defines the data segment of a program containing data
.TEXT directive
Defines the code segment of a program containing instructions
.GLOBL directive
Declares a symbol as global
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 14
Layout of a Program in Memory
0x7FFFFFFF
Stack Segment Stack Grows
Downwards
Memory
Addresses
in Hex
Dynamic Area (Heap) Data Segment
Reserved
0
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 15
Next . . .
System Calls
Defining Data
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 16
System Calls
Programs do input/output through system calls
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 20
Reading and Printing a String
################# Data segment #####################
.data
str: .space 10 # array of 10 bytes
################# Code segment #####################
.text
.globl main
main: # main program entry
la $a0, str # $a0 = address of str
li $a1, 10 # $a1 = max string length
li $v0, 8 # read string
syscall
li $v0, 4 # Print string str
syscall
li $v0, 10 # Exit program
syscall
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 21
Sum of Three Integers
# Sum of three integers
# Objective: Computes the sum of three integers.
# Input: Requests three numbers, Output: sum
################### Data segment ###################
.data
prompt: .asciiz "Please enter three numbers: \n"
sum_msg: .asciiz "The sum is: "
################### Code segment ###################
.text
.globl main
main:
la $a0,prompt # display prompt string
li $v0,4
syscall
li $v0,5 # read 1st integer into $t0
syscall
move $t0,$v0
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 22
Sum of Three Integers – (cont'd)
li $v0,5 # read 2nd integer into $t1
syscall
move $t1,$v0
li $v0,5 # read 3rd integer into $t2
syscall
move $t2,$v0
addu $t0,$t0,$t1 # accumulate the sum
addu $t0,$t0,$t2
la $a0,sum_msg # write sum message
li $v0,4
syscall
move $a0,$t0 # output sum
li $v0,1
syscall
li $v0,10 # exit
syscall
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 23
Next . . .
System Calls
Defining Data
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 24
Data Definition Statement
The assembler uses directives to define data
Syntax:
var1: .WORD 10
All initializers become binary data in memory
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 25
Data Directives
.BYTE Directive
Stores the list of values as 8-bit bytes
.HALF Directive
Stores the list as 16-bit values aligned on half-word boundary
.WORD Directive
Stores the list as 32-bit values aligned on a word boundary
.FLOAT Directive
Stores the listed values as single-precision floating point
.DOUBLE Directive
Stores the listed values as double-precision floating point
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 26
String Directives
.ASCII Directive
.ASCIIZ Directive
.SPACE Directive
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 27
Examples of Data Definitions
.DATA
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 28
Next . . .
System Calls
Defining Data
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 29
Memory Alignment
Memory is viewed as an addressable array of bytes
Byte Addressing: address points to a byte in memory
However, words occupy 4 consecutive bytes in memory
MIPS instructions and integers occupy 4 bytes
address
...
Address must be multiple of size
aligned word
Word address should be a multiple of 4 not aligned
12
Double-word address should be a multiple of 8 8
4
.ALIGN n directive 0 not aligned
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 30
Byte Ordering (Endianness)
Processors can order bytes within a word in two ways
Little Endian Byte Ordering
Memory address = Address of least significant byte
Example: Intel IA-32
MSB LSB address a a+1 a+2 a+3
Byte 3 Byte 2 Byte 1 Byte 0 . . . Byte 0 Byte 1 Byte 2 Byte 3 ...
32-bit Register Memory
Big Endian Byte Ordering
Memory address = Address of most significant byte
Example: SPARC architecture
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 31
Symbol Table
Assembler builds a symbol table for labels
Assembler computes the address of each label in data segment
var1 str1
0x10010000 1 2 'Z' 'M' 'y' ' ' 'S' 't' 'r' 'i' 'n' 'g' '\n' 0 0 0 Unused
0x10010010 0x12345678 0 0 0 0 1000
var2 (aligned) Unused var3 (address is multiple of 8)
Introduction to Assembly Language Programming COE 301 – KFUPM © Muhamed Mudawar – slide 32