Practical Session 2 Computer Architecture and Assembly Language.

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman.
Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers
Flow Control Instructions
Practical Session 2. Labels Definition valid characters in labels are: letters, numbers, _, $, ~,., and ? first character can be: letter, _, ? and.
Practical Session 2. Labels Definition valid characters in labels are: letters, numbers, _, $, ~,., and ? first character can be: letter, _, ? and.
Practical Session 1 Computer Architecture and Assembly Language.
Introduction to Assembly Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
LAB Flag Bits and Register
Lecture 5 Multiplication, Division and Branches Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
Lecture 4 ( Assembly Language).
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Arithmetic Flags and Instructions
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
Practical Session 2. Flags Register (Status Register) A flag is a single bit of information whose meaning is independent from any other bit Each flag.
Microprocessor MA Rahim Khan Computer Engineering and Networks Department.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
Assembly Language Wei Gao. Assembler language Instructions.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
CS-401 Computer Architecture & Assembly Language Programming
Computer Architecture and Assembly Language
CSC 221 Computer Organization and Assembly Language
Computer Architecture and Assembly Language
Data Transfers, Addressing, and Arithmetic
Computer Architecture and Assembly Language
Homework Reading Labs PAL, pp
Practical Session 2.
Today we are going to discuss about,
ICS312 SET 7 Flags.
Instruksi Set Prosesor 8088
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Basic Assembly Language
INSTRUCTION SET.
Microprocessor and Assembly Language
Assembly IA-32.
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
CS 301 Fall 2002 Assembly Instructions
Computer Organization and Assembly Language
Lecture 4 ( Assembly Language).
Flags Register & Jump Instruction
فصل پنجم انشعاب و حلقه.
Practical Session 2.
Shift & Rotate Instructions)
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Computer Architecture and System Programming Laboratory
X86 Assembly Review.
Computer Architecture and System Programming Laboratory
Chapter 7 –Program Logic and Control
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Part IV The FLAGS Register
Presentation transcript:

Practical Session 2 Computer Architecture and Assembly Language

EFLAGS - Flags Register (Status Register) flag is a single independent bit of (status) information each flag has a two-letter symbol name Status Flags

CF gets ‘1’ if result is larger than "capacity" of target operand b b = b = 0  CF = 1 In unsigned arithmetic, watch the carry flag to detect errors. In signed arithmetic ignore CF flag b b = unsigned arithmetic  b = 255 (decimal)  got the wrong answer signed arithmetic  b = -1 (decimal)  ignore CF flag CF gets ‘1’ if subtract larger number from smaller (borrow out of “capacity” of target operand) b b = b  CF = 1 otherwise CF gets ‘0’ CF - Carry Flag b b ≡ b b ≡ b

OF – Overflow Flag sign-bit-off operands + sign-bit-on result  OF gets ‘1’ b b = b  OF = 1 sign-bit-on operands + sign-bit-off result  OF gets ‘1’ b b = b = 0  OF = 1 otherwise OF gets ‘0’ b b = b  OF = b b = b  OF = b b = b  OF = b b = b  OF = 0 In signed arithmetic, watch the overflow flag to detect errors – addition of two positive numbers results negative, or addition two negative numbers results positive. In unsigned arithmetic ignore OF flag.

ZF – Zero Flag - set if a result is zero; cleared otherwise add 0, 0  ZF = 1 first nibble second nibble ZF, SF, PF, and AF Flags SF – Sign Flag – set if a result is negative (i.e. MSB of the result is 1) sub b, b  b  SF = 1 PF – Parity Flag - set if low-order eight bits of result contain an even number of "1" bits; cleared otherwise add b, 1000 b (result is b  4 bits are ‘1’  PF = 1) add b, 1000 b (result is b  3 bits are ‘1’  PF = 0) AF – Auxiliary Carry Flag (or Adjust Flag) is set if there is a carry from low nibble (4 bits) to high nibble or a borrow from a high nibble to low nibble 1111 b b = b  AF = b b = b b = 1111 b  AF = 1

InstructionDescriptionFlags JO Jump if overflowOF = 1 JNO Jump if not overflowOF = 0 JS Jump if signSF = 1 JNS Jump if not signSF = 0 JE JZ Jump if equal Jump if zero ZF = 1 JNE JNZ Jump if not equal Jump if not zero ZF = 0 JB JNAE JC Jump if below Jump if not above or equal Jump if carry CF = 1 JNB JAE JNC Jump if not below Jump if above or equal Jump if not carry CF = 0 JBE JNA Jump if below or equal Jump if not above CF = 1 or ZF = 1 JA JNBE Jump if above Jump if not below or equal CF = 0 and ZF = 0 JL JNGE Jump if less Jump if not greater or equal SF <> OF JGE JNL Jump if greater or equal Jump if not less SF = OF JLE JNG Jump if less or equal Jump if not greater ZF = 1 or SF <> OF JG JNLE Jump if greater Jump if not less or equal ZF = 0 and SF = OF JP JPE Jump if parity Jump if parity even PF = 1 JNP JPO Jump if not parity Jump if parity odd PF = 0 JCXZ JECXZ Jump if CX register is 0 Jump if ECX register is 0 CX = 0 ECX = 0 Jcc: Conditional Branch

MOVNOTJMP * does not affect flags NEG The CF flag set to 0 if the source operand is 0; otherwise it is set to 1. The OF, SF, ZF, AF, and PF flags are set according to the result. ANDOR The OF and CF flags are cleared; the SF, ZF, and PF flags are set according to the result. The state of the AF flag is undefined DECINC The CF flag is not affected. The OF, SF, ZF, AF, and PF flags are set according to the result ADDADCSUBSBB The OF, SF, ZF, AF, PF, and CF flags are set according to the result. CMP Modify status flags in the same manner as the SUB instruction The full set of instructions can be found here.here Instructions seen till now – affecting flags

Byte Order - Little Endian least significant byte smallest address Little Endian - stores the least significant byte in the smallest address. 0x78 0x56 0x34 0x12 [var] [var+1] [var+2] [var+3] 0x780x560x340x12 EAX register effective address numeric into memory  reversed order var: dd 0x ; 0x78 0x56 0x34 0x12 numeric into register  source order mov EAX, 0x ; 0x12 0x34 0x56 0x78 characters into memory  source order var: dw ‘abc’ ; 0x61 0x62 0x63 0x00 characters into register  reversed order mov eax, ‘abc’ ; 0x00 0x63 0x62 0x61 register into memory  reversed order mov [buffer], eax ; 0x61 0x62 0x63 0x00 memory into register  reversed order mov eax, [buffer] ; 0x00 0x63 0x62 0x61 0x61 0x62 0x63 0 [var] [var+1] [var+2] [var+3] 0x610x620x630 EAX register

Sections Every process consists of sections that are accessible to the process during its execution. char a[10]; int a = 1; const int i = 10; main() { int aL=1, cL; char * ptr = malloc(4); cL= a+aL+i; } section.bss BUFFLEN equ 1024 buff: resb BUFFLEN section.rodata LC0: db 'Hello world!' section.data an: dd 0 section.text mov ax, 2... Examples : Data.bss - holds uninitialized data; occupies no space.data and.data1 - holds initialized data.rodata and.rodata1 - holds read-only data Text.text – holds executable instructions of a program Stack – contains information + local variables that is saved for each function call Heap – contains dynamically allocated memory ---->.bss ---->.data ---->.rodata ----> local variables on Stack ----> allocated memory in Heap ---->.text

Memory layout for Linux Read-only Data Segment.bss.data.text.rodata USER Space Virtual Addresses loaded from.exe file program (.exe file) creates its own memory space in the RAM  process

Pseudo-instructions - RES - declare uninitialized storage space Examples: buffer: resb 64 ; reserve 64 bytes wordVar: resw 1 ; reserve a word realArray: resq 10 ; array of ten quadwords (8 bytes) Note: you can not make any assumption about content of a storage space cells. Pseudo-instructions - EQU - define constant Example: foo: EQU 1 ; foo = 1 Note: the value of foo cannot be changed.

Pseudo-instructions - TIMES - repeat (instruction or data) TIMES prefix causes the instruction to be assembled multiple times zeroBuf: times 64 db 0 ; 64 bytes initialized to 0 ! … ! ! o g … $$ (start of the current section) - start of section.data buffer $ (current position in section) buffer … … 20 TIMES can be applied to ordinary instructions, so you can code trivial loops mov EAX, 0 times 100 inc EAX ; EAX =100 => loop the argument to TIMES is not just a numeric constant, but a numeric expression 64-$+buffer buffer: db ‘go’ times 64-$+buffer db ‘!’ ; (64 - $ + buffer = 64 – = 64 – 2 = 62)

Advanced Instructions - multiplication unsigned MUL r/m - unsigned integer multiplication signed IMUL r/m - signed integer multiplication MultiplicandMultiplierProduct ALr/m8AX r/m16DX:AX EAXr/m32EDX:EAX MUL r/m16 mov bx, 8000 h mov ax, 2000 h mul bx ; dx:ax  1000:0000 h MUL r/m32 mov ebx, h mov eax, h mul ebx ; edx:eax  : h MUL r/m8 mov bl,5 ; multiplier mov al,9 ; multiplicand mul bl ; ax  2D h