# Assembly Programming Notes for Practical2 Munaf Sheikh

## Presentation on theme: "Assembly Programming Notes for Practical2 Munaf Sheikh"— Presentation transcript:

Assembly Programming Notes for Practical2 Munaf Sheikh http://www.cs.uwc.ac.za/~msheikh/COS365Assembler

Stub Program

Data representation Electrical current has two states (on or off) Electrical current has two states (on or off) Binary used to represent the states Binary used to represent the states (1 = on, 0 = off)  bits 100100100111010110100100100100001 Octal (0 to 8)  bytes Octal (0 to 8)  bytes Hexadecimal (0 to F) used when binary strings get TOO long Hexadecimal (0 to F) used when binary strings get TOO long

Operations on binary Add Add –101 ADD 110 = 011 overflow 1 Subtract Subtract –101 SUB 110 = 1111 1111 And And –101 AND 110 = 100 Or Or –101 OR 110 = 111 Not Not –Not 101 = 010

CPU Registers Central Processing Unit (CPU) ALUCUCLOCK IO Devices #1… #N Memory Storage Unit Control bus Address bus Data bus

Registers Registers are named locations within the CPU that can be accessed VERY quickly Registers are named locations within the CPU that can be accessed VERY quickly –General Purpose –Segment –Pointer –Index

General-Purpose Registers AX Accumulator Register mostly used for calculations and for input/output AX Accumulator Register mostly used for calculations and for input/output BX Base Register Only register that can be used as an index BX Base Register Only register that can be used as an index CX Count Register register used for the loop instruction CX Count Register register used for the loop instruction DX Data Register input/output and used by multiply and divide DX Data Register input/output and used by multiply and divide

Pointer Registers IP Instruction Pointer 16-bit number that points to the offset of the next instruction IP Instruction Pointer 16-bit number that points to the offset of the next instruction SP Stack Pointer 16-bit number that points to the offset that the stack is using SP Stack Pointer 16-bit number that points to the offset that the stack is using BP Base Pointer used to pass data to and from the stack BP Base Pointer used to pass data to and from the stack

Segment Registers CS Code Segment 16-bit number that points to the active code-segment CS Code Segment 16-bit number that points to the active code-segment DS Data Segment 16-bit number that points to the active data-segment DS Data Segment 16-bit number that points to the active data-segment SS Stack Segment 16-bit number that points to the active stack-segment SS Stack Segment 16-bit number that points to the active stack-segment ES Extra Segment 16-bit number that points to the active extra-segment ES Extra Segment 16-bit number that points to the active extra-segment

Index Registers SI Source Index used by string operations as source SI Source Index used by string operations as source DI Destination Index used by string operations as destination DI Destination Index used by string operations as destination

Basic Elements of Assembly Integer Constants Integer Constants 26; 26d (decimal); 42o (octal); 1Ah (hex) Integer Expressions Integer Expressions -(3+4) * (6-1) Real Number Constants Real Number Constants 2.0; +3.0; -44.2E+05; 26E5 Character Constants Character Constants ‘A’; “d” String Constants String Constants ‘ABC’; ‘X’; “Hello World”; ‘4096’;

Basic Elements (cnt..) Reserved Words Reserved Words –Instruction mnemonics –Directives –Attributes –Operators –Predefined Symbols Identifiers Identifiers Programmer chosen name: identifier, constant, procedure, code lable Directives Directives.data;.code; …

Basic Elements (cnt..) Instructions Instructions –Label (optional) –Instruction Mnemonic (required) –Operands (usually required) –Comment (optional) Eg: Eg: target: mov ax, bx …. jmp target Eg: mov ax, myVariable Eg: mov ax, myVariable Label:MnemonicOperands ; Comment

Intel Instruction set Add dest, src Add dest, src Sub dest, src Sub dest, src And dest, src And dest, src Or dest, src Or dest, src Not dest Not dest Mul src Mul src http://www.penguin.cz/~literakl/intel/intel.html http://www.penguin.cz/~literakl/intel/intel.html

Prac 2 Irvine16 library provides functions that can be used in your programs Irvine16 library provides functions that can be used in your programs Declaring prototypes for library functions Declaring prototypes for library functions Crlf PROTO Readstring PROTO Readint PROTO Writestring PROTO Writeint PROTO

Prac 2 (cnt..) – writing strings userprompt db "What's your name:",0 userprompt db "What's your name:",0 ; prompt for uname (.data) mov dx, offset userprompt mov dx, offset userprompt ; point dx, to address of for display Invoke Writestring Invoke Writestring ; display (function in IRVINE library) dx: data register, used for input/output of text

Prac 2 (Cnt..) – writing ints anumber dw ? anumber dw ? ; integer (.data) mov ax, anumber mov ax, anumber ; copy to AX register for display invoke Writeint invoke Writeint ; display AX register as signed-integer ax: accumulator register, used for calculations and input/output

Prac 2 (cnt..) – reading strings uname db 50 dup(0) uname db 50 dup(0) ; string (.data) mov dx, offset uname mov dx, offset uname ; point dx, to address of input var ; point dx, to address of input var invoke Readstring ; read input as string into ; read input as string into

Prac 2 (cnt..) – reading ints anumber dw ? anumber dw ? ; integer (.data) invoke Readint ; read input as 16bit integer (input in register AX) mov anumber, ax mov anumber, ax ; copy read in integer into ; copy read in integer into

Due date: Not later than 23:59:59.99999 22/02/2005 Not later than 23:59:59.99999 22/02/2005