210 likes | 327 Views
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) Binary used to represent the states (1 = on, 0 = off) bits 100100100111010110100100100100001
E N D
Assembly Programming Notes for Practical2 Munaf Sheikh http://www.cs.uwc.ac.za/~msheikh/COS365Assembler
Data representation • Electrical current has two states (on or off) • Binary used to represent the states (1 = on, 0 = off) bits 100100100111010110100100100100001 • Octal (0 to 8) bytes • Hexadecimal (0 to F) used when binary strings get TOO long
Operations on binary • Add • 101 ADD 110 = 011 overflow 1 • Subtract • 101 SUB 110 = 1111 1111 • And • 101 AND 110 = 100 • Or • 101 OR 110 = 111 • Not • Not 101 = 010
ALU CU CLOCK CPU Data bus Registers Memory Storage Unit IO Devices #1… #N Central Processing Unit (CPU) Control bus Address bus
Registers • Registers are named locations within the CPU that can be accessed VERY quickly • General Purpose • Segment • Pointer • Index
General-Purpose Registers • AXAccumulator Register mostly used for calculations and for input/output • BXBase Register Only register that can be used as an index • CXCount Register register used for the loop instruction • DXData 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 • SPStack Pointer 16-bit number that points to the offset that the stack is using • BPBase Pointer used to pass data to and from the stack
Segment Registers • CSCode Segment 16-bit number that points to the active code-segment • DSData Segment 16-bit number that points to the active data-segment • SSStack Segment 16-bit number that points to the active stack-segment • ES Extra Segment 16-bit number that points to the active extra-segment
Index Registers • SISource Index used by string operations as source • DIDestination Index used by string operations as destination
Basic Elements of Assembly • Integer Constants 26; 26d (decimal); 42o (octal); 1Ah (hex) • Integer Expressions -(3+4) * (6-1) • Real Number Constants 2.0; +3.0; -44.2E+05; 26E5 • Character Constants ‘A’; “d” • String Constants ‘ABC’; ‘X’; “Hello World”; ‘4096’;
Basic Elements (cnt..) • Reserved Words • Instruction mnemonics • Directives • Attributes • Operators • Predefined Symbols • Identifiers Programmer chosen name: identifier, constant, procedure, code lable • Directives .data; .code; …
Label: Mnemonic Operands ; Comment Basic Elements (cnt..) • Instructions • Label (optional) • Instruction Mnemonic (required) • Operands (usually required) • Comment (optional) • Eg: target: mov ax, bx …. jmp target • Eg: mov ax, myVariable
Intel Instruction set • Add dest, src • Sub dest, src • And dest, src • Or dest, src • Not dest • Mul src • http://www.penguin.cz/~literakl/intel/intel.html
Prac 2 • Irvine16 library provides functions that can be used in your programs • 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 ; prompt for uname (.data) mov dx, offset userprompt ; point dx, to address of <userprompt> for display InvokeWritestring ; display <userprompt> (function in IRVINE library) dx: data register, used for input/output of text
Prac 2 (Cnt..) – writing ints anumber dw ? ; integer <anumber> (.data) mov ax, anumber ; copy <anumber> to AX register for display 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) ; string <uname> (.data) mov dx, offset uname ; point dx, to address of input var <uname> invoke Readstring ; read input as string into <uname>
Prac 2 (cnt..) – reading ints anumber dw ? ; integer <anumber> (.data) invoke Readint ; read input as 16bit integer (input in register AX) mov anumber, ax ; copy read in integer into <anumber>
Due date: • Not later than 23:59:59.99999 22/02/2005