Basic Assembly Language

Slides:



Advertisements
Similar presentations
NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
Advertisements

Department of Computer Science and Software Engineering
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Shift and Rotate Instructions
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
Multiplication and Division Instructions & the 0Ah function.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Arithmetic Flags and Instructions
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
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.
Arithmetic Flags and Instructions Chapter 7 S. Dandamudi.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
Multiplication and Division instructions Dr.Hadi AL Saadi.
Practical Session 2 Computer Architecture and Assembly Language.
CS2422 Assembly Language and System Programming 0 Week 9 Data Transfers, Addressing, and Arithmetic.
Computer Architecture and Assembly Language
Data Transfers, Addressing, and Arithmetic
Homework Reading Labs PAL, pp
Practical Session 2.
8086 Microprocessor.
Microprocessor and Assembly Language
Chapter 3 Bit Operations
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Multiplication and Division Instructions
Assembly IA-32.
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
CS 301 Fall 2002 Assembly Instructions
X86’s instruction sets.
Introduction to Assembly Language
Chapter 4: Instructions
BIC 10503: COMPUTER ARCHITECTURE
Flags Register & Jump Instruction
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Data Transfers, Addressing, and Arithmetic
Digital Logic & Design Lecture 02.
Shift & Rotate Instructions)
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Multiplication and Division Instructions
Shift & Rotate Instructions)
Computer Architecture CST 250
Computer Architecture and System Programming Laboratory
EECE.3170 Microprocessor Systems Design I
X86 Assembly Review.
Chapter 5 Arithmetic and Logic Instructions
Multiplication and Division Instructions
Multiplication and Division Instructions
Chapter 8: Instruction Set 8086 CPU Architecture
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Basic Assembly Language

Outline Working with integers Control structures Translating standard control structures Example: finding prime numbers

Working with integers Integer representation Sign extension Two’s complement arithmetic Example program Extended precision arithmetic

Integer representation Integers come in two flavors: unsigned and signed. Unsigned integers are represented in a very straightforward binary manner. Three general techniques that have been used to represent signed integers. All of these methods use the most significant bit of the integer as a sign bit. This bit is 0 if the number is positive and 1 if negative. Signed magnitude One’s complement Two’s complement

Signed magnitude(原码) The first method is the simplest and is called signed magnitude. E.g., 56 would be represented as the byte 00111000 and −56 would be 10111000. drawbacks two possible values of zero, +0 (00000000) and −0 (10000000). general arithmetic is also complicated. E.g., If 10 is added to −56, this must be recast as 10 subtracted by 56.

One’s complement(反码) 正数的反码=正数的原码 负数的反码=原码(符号位除外)各位取反而得到的 As for the first method, there are two representations of zero: 00000000 (+0) and 11111111 (−0). 例如:X1= +1010110 X2 = 一1001010 [X1]原=01010110 [X1]反=[X1]原=01010110 [X2]原=11001010 [X2]反=10110101

Two’s complement(补码) 正数的补码=正数的原码 负数的补码=负数的反码加1 例如,[X1]=+1010110 即 [X1]原=[X1]补=01010110 [X2] 原= 11001010 [X2] 补=10110101+1=10110110

原码、反码和补码 十进制 原码 反码 补码 + 0 0000 0000B 0000 0000B 0000 0000B

Sign extension: Decreasing size of data The rule for unsigned numbers is that all the bits being removed must be 0 for the conversion to be correct. The rule for signed numbers is that the bits being removed must be either all 1’s or all 0’s. The first bit not being removed must have the same value as the removed bits. mov ax, 0034h ; ax = 52 (stored in 16 bits) mov cl, al ;cl = lower 8-bits of ax

Sign extension: Increasing size of data In general, to extend an unsigned number, one makes all the new bits of the expanded number 0. e.g., FF becomes 00FF. to extend a signed number, one must extend the sign bit. This means that the new bits become copies of the sign bit.

MOVZX For unsigned numbers This instruction has two operands. The destination (first operand) must be a 16 or 32 bit register. The source (second operand) may be an 8 or 16 bit register or a byte or word of memory. The other restriction is that the destination must be larger than the source. For example, to extend the byte in AL to an unsigned word in AX: mov ah, 0 ; zero out upper 8-bits However, it is not possible to use a MOV instruction to convert the unsigned word in AX to an unsigned double word in EAX. Why not? There is no way to specify the upper 16 bits of EAX in a MOV. movzx eax, ax ; extends ax into eax movzx eax, al ; extends al into eax movzx ax, al ; extends al into ax movzx ebx, ax ; extends ax into ebx

CBW, CWD, CDQ, CWDE: Sign Extensions All these instructions sign-extend a short value into a longer one, by replicating the top bit of the original value to fill the extended one. CBW extends AL into AX by repeating the top bit of AL in every bit of AH. CWDE extends AX into EAX. CWD extends AX into DX:AX by repeating the top bit of AX throughout DX, and CDQ extends EAX into EDX:EAX.

Application to C programming unsigned char uchar = 0xFF; signed char schar = 0xFF; int a = (int ) uchar ; /* a = 255 (0x000000FF) */ int b = (int ) schar ; /* b = −1 (0xFFFFFFFF) */ However, there is one value that it may return that is not a character, EOF. This is a macro that is usually defined as −1. Thus, fgetc() either returns back a char extended to an int value (which looks like 000000xx in hex) or EOF (which looks like FFFFFFFF in hex). int fgetc( FILE * ); char ch; while( (ch = fgetc(fp)) != EOF ) { /* do something with ch */ }

Layout of the FLAGS Register (Lower 16 bits of EFLAGS) condition codes The EFLAGS register is a 32-bit register that encapsulates several single-bit boolean (true/false) values. Most of the bits in the EFLAGs register are either reserved for kernel mode (operating system) functions, or are of little interest to the application programmer. Eight of these bits (or flags) are of interest to application programmers writing assembly language programs. These are the overflow, direction, interrupt disable1, sign, zero, auxiliary carry, parity, and carry flags. The following diagram shows their layout within the lower 16-bits of the EFLAGS register . Of the eight flags that are usable by application programmers, four flags in particular are extremely valuable: the overflow, carry, sign, and zero flags. Collectively, we will call these four flags the condition codes2 The last register of interest is the EIP (instruction pointer) register. This 32-bit register contains the memory address of the next machine instruction to execute. Although you will manipulate this register directly in your programs, the instructions that modify its value treat this register as an implicit operand. Therefore, you will not need to remember much about this register since the 80x86 instruction set effectively hides it from you. All the 80x86 registers have their own special purposes that limit their use in certain contexts. For the time being, you should simply avoid the use of the ESP and EBP registers for generic calculations and keep in mind that the remaining registers are not completely interchangeable in your programs. carry

Two’s complement arithmetic ADD and SUB instructions Two of the bits in the FLAGS register that these instructions set are the overflow and carry flag. MUL or IMUL instructions Consider the multiplication of the byte FF with itself yielding a word result. Using unsigned multiplication this is 255 times 255 or 65025. Using signed multiplication this is −1 times −1 or 1 mul source

Mul & IMUL: mul source mul (Unsigned Integer Multiply ) If the operand is byte sized, it is multiplied by the byte in the AL register and the result is stored in the 16 bits of AX. If the source is 16-bit, it is multiplied by the word in AX and the 32-bit result is stored in DX:AX. If the source is 32-bit, it is multiplied by EAX and the 64-bit result is stored into EDX:EAX. Mul & IMUL: mul source mul (Unsigned Integer Multiply ) The source is either a register or a memory reference. (not an immediate value) Exactly what multiplication is performed depends on the size of the source operand. imul (Signed Integer Multiply ) the same formats as mul, also adds some other instruction formats. imul dest, source1 imul dest, source1, source2 If the operand is byte sized, it is multiplied by the byte in the AL register and the result is stored in the 16 bits of AX. If the source is 16-bit, it is multiplied by the word in AX and the 32-bit result is stored in DX:AX. If the source is 32-bit, it is multiplied by EAX and the 64-bit result is stored into EDX:EAX.

DIV & IDIV: div source If the source is 8-bit, then AX is divided by the operand. The quotient is stored in AL and the remainder in AH. If the source is 16-bit, then DX:AX is divided by the operand. The quotient is stored into AX and remainder into DX. If the source is 32-bit, then EDX:EAX is divided by the operand and the quotient is stored into EAX and the remainder into EDX. A very common error is to forget to initialize DX or EDX before division.

NEG instruction The NEG instruction negates it’s single operand by computing its two’s complement. Its operand may be any 8-bit, 16-bit, or 32-bit register or memory location. with n bits we can represent the signed values in the range -2n-1 to +2n-1-1

Example program Math.asm

Extended precision arithmetic Both the ADD and SUB instructions modify the carry flag if a carry or borrow are generated, respectively. This information stored in the carry flag can be used to add or subtract large numbers by breaking up the operation into smaller double word (or smaller) pieces.

ADC & SBB ADC operand1 = operand1 + carry flag + operand2 SBB operand1 = operand1 - carry flag - operand2 E.g.,Consider the sum of 64-bit integers in EDX:EAX and EBX:ECX. For really large numbers, a loop could be used add eax, ecx ; add lower 32-bits adc edx, ebx ; add upper 32-bits and carry from previous sum

Control structures Comparisons Branch instructions The loop instructions It uses the infamous goto and used inappropriately can result in spaghetti code! However, it is possible to write structured assembly language programs. The basic procedure is to design the program logic using the familiar high level control structures and translate the design into the appropriate assembly language (much like a compiler would do).

Comparisons cmp vleft, vright For unsigned integers, the zero (ZF) and carry (CF) flags. The zero flag is set (1) if the resulting difference would be zero. The carry flag is used as a borrow flag for subtraction.

Comparisons For signed integers, the zero(ZF) flag, the overflow (OF) flag and the sign (SF) flag. The overflow flag is set if the result of an operation overflows (or underflows). The sign flag is set if the result of an operation is negative. If vleft = vright, the ZF is set (just as for unsigned integers). If vleft > vright, ZF is unset and SF = OF. If vleft < vright, ZF is unset and SF != OF.

Branch instructions unconditional and conditional. An unconditional branch is just like a goto, it always makes the branch. A conditional branch may or may not make the branch depending on the flags in the FLAGS register. If a conditional branch does not make the branch, control passes to the next instruction.

Jump The JMP (short for jump) instruction makes unconditional branches. Its single argument is usually a code label to the instruction to branch to. SHORT, this jump is very limited in range. It can only move up or down 128 bytes in memory. Near, this jump is the default type for both unconditional and conditional branches Far, this jump allows control to move to another code segment.

Simple Conditional Branches JZ branches only if ZF is set JNZ branches only if ZF is unset JO branches only if OF is set JNO branches only if OF is unset JS branches only if SF is set JNS branches only if SF is unset JC branches only if CF is set JNC branches only if CF is unset JP branches only if PF is set JNP branches only if PF is unset

Example 1

Example 2

Signed and Unsigned Comparison Instructions cmp eax, 5 jge thenblock mov ebx, 2 jmp next thenblock: mov ebx, 1 next:

The loop instructions LOOP Decrements ECX, if ECX != 0, branches to label LOOPE, LOOPZ Decrements ECX (FLAGS register is not modified), if ECX != 0 and ZF = 1, branches LOOPNE, LOOPNZ Decrements ECX (FLAGS unchanged), if ECX !=0 and ZF = 0, branches

Translating standard control structures If statements While loops Do while loops If vleft = vright, the ZF is set (just as for unsigned integers). If vleft > vright, ZF is unset and SF = OF. If vleft < vright, ZF is unset and SF 6= OF.

If statements

While loops

Do while loops

Example: finding prime numbers 大于1,并且除1和它本身外没有其他因数的自然数叫素数(或质数). E.g.:2,3,5,7,11,13等都是素数。 判断一个数是素数 所有的偶数不是素数?奇数是不是都是素数? 用3-本数的平方根之间所有的奇数去除本数,如能除尽就不是素数,不能除尽就是素数。

C program

Asm progam Prime.asm

Summary Working with integers Control structures Translating standard control structures Example: finding prime numbers