Introduction to Assembly Language Programming COE 301 Computer
Introduction to Assembly Language Programming COE 301 Computer Organization ICS 233 Computer Architecture and Assembly Language Dr. Marwan Abu-Amara College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals [Adapted from slides of Dr. M. Mudawar and Dr. A. El-Maleh, KFUPM]
Outline v MIPS Instruction Set Architecture v Overview of the MIPS Processor v Assembly Language Statements v Assembly Language Program Template v Defining Data v Memory Alignment and Byte Ordering v System Calls Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 2
Instruction Set Architecture (ISA) v Critical interface between hardware and software v An ISA includes the following … ² Instructions and Instruction Formats § Data Types, Encodings, and Representations § Addressing Modes: to address Instructions and Data § Handling Exceptional Conditions (like division by zero) ² Programmable Storage: Registers and Memory v Examples (Versions) First Introduced in ² Intel (8086, 80386, Pentium, . . . ) 1978 ² MIPS (MIPS I, III, IV, V) 1986 ² Power. PC (601, 604, …) 1993 Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 3
Instructions v Instructions are the language of the machine v We will study the MIPS instruction set architecture ² Known as Reduced Instruction Set Computer (RISC) ² Elegant and relatively simple design ² Similar to RISC architectures developed in mid-1980’s and 90’s ² Very popular, used in many products § Silicon Graphics, ATI, Cisco, Sony, etc. ² Comes next in sales after Intel IA-32 processors § Almost 100 million MIPS processors sold in 2002 (and increasing) v Alternative design: Intel IA-32 ² Known as Complex Instruction Set Computer (CISC) Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 4
Next. . . v Instruction Set Architecture v Overview of the MIPS Processor v Assembly Language Statements v Assembly Language Program Template v Defining Data v Memory Alignment and Byte Ordering v System Calls Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 5
Overview of the MIPS Processor. . . Memory 4 bytes per word Up to 232 bytes = 230 words. . . EIU $0 $1 $2 32 General Purpose Registers Arithmetic & Logic Unit $31 ALU Execution & Integer Unit (Main proc) Integer mul/div Hi Lo Integer Multiplier/Divider Instruction Set Architecture COE 301 – Computer Organization – KFUPM FPU FP Arith TMU F 0 F 1 F 2 Floating Point Unit (Coproc 1) 32 Floating-Point Registers F 31 Floating-Point Arithmetic Unit Bad. Vad Trap & dr Status Memory Unit Cause (Coproc 0) EPC © Muhamed Mudawar slide 6
MIPS General-Purpose Registers v 32 General Purpose Registers (GPRs) ² Assembler uses the dollar notation to name registers § $0 is register 0, $1 is register 1, …, and $31 is register 31 ² All registers are 32 -bit wide in MIPS 32 $0 = $zero $16 = $s 0 ² Register $0 is always zero $1 = $at $17 = $s 1 $2 = $v 0 $18 = $s 2 $3 = $v 1 $19 = $s 3 $4 = $a 0 $20 = $s 4 $5 = $a 1 $21 = $s 5 $6 = $a 2 $22 = $s 6 $7 = $a 3 $23 = $s 7 $8 = $t 0 $24 = $t 8 $9 = $t 1 $25 = $t 9 $10 = $t 2 $26 = $k 0 $11 = $t 3 $27 = $k 1 $12 = $t 4 $28 = $gp $13 = $t 5 $29 = $sp $14 = $t 6 $30 = $fp $15 = $t 7 $31 = $ra § Any value written to $0 is discarded v Software conventions ² Software defines names to all registers § To standardize their use in programs ² Example: $8 - $15 are called $t 0 - $t 7 § Used for temporary values Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 7
MIPS Register Conventions v Assembler can refer to registers by name or by number ² It is easier for you to remember registers by name ² Assembler converts register name to its corresponding number Name $zero $at $v 0 – $v 1 $a 0 – $a 3 $t 0 – $t 7 $s 0 – $s 7 $t 8 – $t 9 $k 0 – $k 1 $gp $sp $fp $ra Instruction Set Architecture Register $0 $1 $2 – $3 $4 – $7 $8 – $15 $16 – $23 $24 – $25 $26 – $27 $28 $29 $30 $31 Usage Always 0 (forced by hardware) Reserved for assembler use Result values of a function Arguments of a function Temporary Values Saved registers (preserved across call) More temporaries Reserved for OS kernel Global pointer (points to global data) Stack pointer Frame pointer Return address COE 301 – Computer Organization – KFUPM (points to top of stack) (points to stack frame) (used by jal for function call) © Muhamed Mudawar slide 8
Instruction Formats v All instructions are 32 -bit wide. Three instruction formats: v Register (R-Type) ² Register-to-register instructions ² Op: operation code specifies the format of the instruction Op 6 Rs 5 Rt 5 Rd 5 sa 5 v Immediate (I-Type) ² 16 -bit immediate constant is part in the instruction Op 6 Rs 5 Rt 5 immediate 16 v Jump (J-Type) ² Used by jump instructions Op 6 Instruction Set Architecture immediate 26 COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 9 funct 6
Instruction Categories v Integer Arithmetic ² Arithmetic, logical, and shift instructions v Data Transfer ² Load and store instructions that access memory ² Data movement and conversions v Jump and Branch ² Flow-control instructions that alter the sequential sequence v Floating Point Arithmetic ² Instructions that operate on floating-point registers v Miscellaneous ² Instructions that transfer control to/from exception handlers ² Memory management instructions Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 10
Next. . . v Instruction Set Architecture v Overview of the MIPS Processor v Assembly Language Statements v Assembly Language Program Template v Defining Data v Memory Alignment and Byte Ordering v System Calls Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 11
Assembly Language Statements v Three types of statements in assembly language ² Typically, one statement should appear on a line 1. Executable Instructions ² Generate machine code for the processor to execute at runtime ² Instructions tell the processor what to do 2. Pseudo-Instructions and Macros ² Translated by the assembler into real instructions ² Simplify the programmer task 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 Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 12
Instructions v Assembly language instructions have the format: [label: ] mnemonic [operands] [#comment] v Label: (optional) ² Marks the address of a memory location, must have a colon ² Typically appear in data and text segments v Mnemonic ² Identifies the operation (e. g. add, sub, etc. ) v Operands ² Specify the data required by the operation ² Operands can be registers, memory variables, or constants ² Most instructions have three operands L 1: addiu $t 0, 1 Instruction Set Architecture COE 301 – Computer Organization – KFUPM #increment $t 0 © Muhamed Mudawar slide 13
Comments v Comments are very important! ² Explain the program's purpose ² When it was written, revised, and by whom ² Explain data used in the program, input, and output ² Explain instruction sequences and algorithms used ² Comments are also required at the beginning of every procedure § Indicate input parameters and results of a procedure § Describe what the procedure does v Single-line comment ² Begins with a hash symbol # and terminates at end of line Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 14
Next. . . v Instruction Set Architecture v Overview of the MIPS Processor v Assembly Language Statements v Assembly Language Program Template v Defining Data v Memory Alignment and Byte Ordering v System Calls Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 15
Program Template # Title: Filename: # Author: Date: # Description: # Input: # Output: ######### Data segment ###########. data. . . ######### Code segment ###########. text. globl main: # main program entry. . . li $v 0, 10 # Exit program syscall Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 16
. DATA, . TEXT, &. GLOBL Directives v. DATA directive ² Defines the data segment of a program containing data ² The program's variables should be defined under this directive ² Assembler will allocate and initialize the storage of variables v. TEXT directive ² Defines the code segment of a program containing instructions v. GLOBL directive ² Declares a symbol as global ² Global symbols can be referenced from other files ² We use this directive to declare main procedure of a program Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 17
Layout of a Program in Memory 0 x 7 FFFFFFF Memory Addresses in Hex Stack Segment Dynamic Area 0 x 10000000 Stack Grows Downwards Data Segment Static Area Text Segment 0 x 04000000 0 Instruction Set Architecture Reserved COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 18
Next. . . v Instruction Set Architecture v Overview of the MIPS Processor v Assembly Language Statements v Assembly Language Program Template v Defining Data v Memory Alignment and Byte Ordering v System Calls Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 19
Data Definition Statement v Sets aside storage in memory for a variable v May optionally assign a name (label) to the data v Syntax: [name: ] directive initializer [, initializer]. . . var 1: . WORD 10 v All initializers become binary data in memory Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 20
Data Directives v. BYTE Directive ² Stores the list of values as 8 -bit bytes v. HALF Directive ² Stores the list as 16 -bit values aligned on half-word boundary v. WORD Directive ² Stores the list as 32 -bit values aligned on a word boundary v. WORD w: n Directive ² Stores the 32 -bit value w into n consecutive words aligned on a word boundary. Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 21
Data Directives v. HALF w: n Directive ² Stores the 16 -bit value w into n consecutive half-words aligned on a half-word boundary. v. BYTE w: n Directive ² Stores the 8 -bit value w into n consecutive bytes. v. FLOAT Directive ² Stores the listed values as single-precision floating point v. DOUBLE Directive ² Stores the listed values as double-precision floating point Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 22
String Directives v. ASCII Directive ² Allocates a sequence of bytes for an ASCII string v. ASCIIZ Directive ² Same as. ASCII directive, but adds a NULL char at end of string ² Strings are null-terminated, as in the C programming language v. SPACE n Directive ² Allocates space of n uninitialized bytes in the data segment v Special characters in strings follow C convention ² Newline: n Instruction Set Architecture Tab: t COE 301 – Computer Organization – KFUPM Quote: ” © Muhamed Mudawar slide 23
Examples of Data Definitions. DATA var 1: . BYTE 'A', 'E', 127, -1, 'n' var 2: . HALF -10, 0 xffff var 3: . WORD 0 x 12345678 Var 4: . WORD 0: 10 var 5: . FLOAT 12. 3, -0. 1 var 6: . DOUBLE 1. 5 e-10 str 1: . ASCII "A Stringn" str 2: . ASCIIZ "NULL Terminated String" array: . SPACE Instruction Set Architecture 100 COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 24
Next. . . v Instruction Set Architecture v Overview of the MIPS Processor v Assembly Language Statements v Assembly Language Program Template v Defining Data v Memory Alignment and Byte Ordering v System Calls Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 25
Memory Alignment v Memory is viewed as an array of bytes with addresses ² Byte Addressing: address points to a byte in memory v Words occupy 4 consecutive bytes in memory Memory v Alignment: address is a multiple of size ² Word address should be a multiple of 4 § Least significant 2 bits of address should be 00 ² Halfword address should be a multiple of 2 address ² MIPS instructions and integers occupy 4 bytes 12 . . . aligned word not aligned 8 4 0 v. ALIGN n directive ² Aligns the next data definition on a 2 n byte boundary Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 26 not aligned
Symbol Table v Assembler builds a symbol table for labels (variables) ² Assembler computes the address of each label in data segment v Example. DATA var 1: str 1: var 2: . ALIGN var 3: Symbol Table . BYTE. ASCIIZ. WORD 3. HALF var 1 1, 2, 'Z' "My Stringn" 0 x 12345678 1000 Label Address var 1 str 1 var 2 var 3 0 x 10010000 0 x 10010003 0 x 10010010 0 x 10010018 str 1 0 x 10010000 1 2 'Z' 'M' 'y' ' ' 'S' 't' 'r' 'i' 'n' 'g' 'n' 0 0 x 10010010 0 x 12345678 0 0 1000 0 0 0 var 2 (aligned) Instruction Set Architecture Unused COE 301 – Computer Organization – KFUPM var 3 (address is multiple of 8) © Muhamed Mudawar slide 27 Unused
Byte Ordering and Endianness v Processors can order bytes within a word in two ways v Little Endian Byte Ordering ² Memory address = Address of least significant byte ² Example: Intel IA-32, Alpha MSB LSB Byte 3 Byte 2 Byte 1 Byte 0 a+1 a+2 a+3 address a. . . Byte 0 Byte 1 Byte 2 Byte 3 32 -bit Register . . . Memory v Big Endian Byte Ordering ² Memory address = Address of most significant byte ² Example: SPARC, PA-RISC MSB LSB Byte 3 Byte 2 Byte 1 Byte 0 a+1 a+2 a+3 address a. . . Byte 3 Byte 2 Byte 1 Byte 0. . . 32 -bit Register Memory v MIPS defaults to Little Endian Byte Ordering v MIPS can operate with both byte orderings Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 28
Next. . . v Instruction Set Architecture v Overview of the MIPS Processor v Assembly Language Statements v Assembly Language Program Template v Defining Data v Memory Alignment and Byte Ordering v System Calls Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 29
System Calls v Programs do input/output through system calls v MIPS provides a special syscall instruction ² To obtain services from the operating system ² Many services are provided in the SPIM and MARS simulators v Using the syscall system services ² Load the service number in register $v 0 ² Load argument values, if any, in registers $a 0, $a 1, etc. ² Issue the syscall instruction ² Retrieve return values, if any, from result registers Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 30
Syscall Services Service $v 0 Arguments / Result Print Integer 1 $a 0 = integer value to print Print Float 2 $f 12 = float value to print Print Double 3 $f 12 = double value to print Print String 4 $a 0 = address of null-terminated string Read Integer 5 Return integer value in $v 0 Read Float 6 Return float value in $f 0 Read Double 7 Return double value in $f 0 Read String 8 $a 0 = address of input buffer $a 1 = maximum number of characters to read Allocate Heap memory 9 $a 0 = number of bytes to allocate Return address of allocated memory in $v 0 Exit Program 10 Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 31
Syscall Services – Cont’d Print Char 11 $a 0 = character to print Read Char 12 Return character read in $v 0 13 $a 0 = address of null-terminated filename string $a 1 = flags (0=read, 1=write, 9=append) $a 2 = mode (ignored) Return file descriptor in $v 0 (negative if error) 14 $a 0 = File descriptor $a 1 = address of input buffer $a 2 = maximum number of characters to read Return number of characters read in $v 0 Write to File 15 $a 0 = File descriptor $a 1 = address of buffer $a 2 = number of characters to write Return number of characters written in $v 0 Close File 16 $a 0 = File descriptor Open File Read from File Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 32
Reading and Printing an Integer ######### Code segment ###########. text. globl main: # main program entry li $v 0, 5 # Read integer syscall # $v 0 = value read move $a 0, $v 0 li $v 0, 1 syscall # $a 0 = value to print # Print integer li $v 0, 10 syscall # Exit program Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 33
Reading and Printing a String ######### Data segment ###########. data str: . space 10 # array of 10 bytes ######### Code segment ###########. text. globl main: # main program entry la $a 0, str # $a 0 = address of str # (note: la is a pseudo instruction) li $a 1, 10 # $a 1 = max string length li $v 0, 8 # read string syscall li $v 0, 4 # Print string str syscall li $v 0, 10 # Exit program syscall Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 34
Program 1: Sum of Three Integers # Sum of three integers # # Objective: Computes the sum of three integers. # Input: Requests three numbers. # Output: Outputs the sum. ########## Data segment ##########. data prompt: . asciiz "Please enter three numbers: n" sum_msg: . asciiz "The sum is: " ########## Code segment ##########. text. globl main: la $a 0, prompt # display prompt string li $v 0, 4 syscall li $v 0, 5 # read 1 st integer into $t 0 syscall move $t 0, $v 0 Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 35
Sum of Three Integers – Slide 2 of 2 li $v 0, 5 syscall move $t 1, $v 0 # read 2 nd integer into $t 1 li $v 0, 5 syscall move $t 2, $v 0 # read 3 rd integer into $t 2 addu # accumulate the sum $t 0, $t 1 $t 0, $t 2 la $a 0, sum_msg li $v 0, 4 syscall # write sum message move $a 0, $t 0 li $v 0, 1 syscall # output sum li $v 0, 10 syscall # exit Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 36
Program 2: Case Conversion # Objective: Convert lowercase letters to uppercase # Input: Requests a character string from the user. # Output: Prints the input string in uppercase. ########## Data segment ###########. data name_prompt: . asciiz "Please type your name: " out_msg: . asciiz "Your name in capitals is: " in_name: . space 31 # space for input string ########## Code segment ###########. text. globl main: la $a 0, name_prompt # print prompt string li $v 0, 4 syscall la $a 0, in_name # read the input string li $a 1, 31 # at most 30 chars + 1 null char li $v 0, 8 syscall Instruction Set Architecture COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 37
Case Conversion – Slide 2 of 2 loop: la $a 0, out_msg li $v 0, 4 syscall la $t 0, in_name # write output message lb $t 1, ($t 0) beqz $t 1, exit_loop # blt $t 1, 'a', no_change bgt $t 1, 'z', no_change addiu $t 1, -32 # sb $t 1, ($t 0) no_change: addiu $t 0, 1 # j loop exit_loop: la $a 0, in_name # li $v 0, 4 syscall li $v 0, 10 # syscall Instruction Set Architecture if NULL, we are done convert to uppercase: 'A'-'a'=-32 increment pointer output converted string exit COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 38
Example of File I/O # Sample MIPS program that writes to a new text file. data file: . asciiz "out. txt" # output filename buffer: . asciiz "Sample text to write". text li $v 0, la $a 0, li $a 1, li $a 2, syscall move $s 6, li $v 0, move $a 0, la $a 1, li $a 2, syscall li $v 0, move $a 0, syscall 13 file 1 0 $v 0 15 $s 6 buffer 20 16 $s 6 Instruction Set Architecture # # # # system call to open a file for writing output file name Open for writing (flags 1 = write) mode is ignored open a file (file descriptor returned in $v 0) save the file descriptor Write to file just opened file descriptor address of buffer from which to write number of characters to write = 20 write to file system call to close file descriptor to close file COE 301 – Computer Organization – KFUPM © Muhamed Mudawar slide 39
- Slides: 39