[asm]lab7

56
Assembly Language (Lab 7) PROCS + New Instructions

Upload: nora-youssef

Post on 12-Apr-2017

281 views

Category:

Education


0 download

TRANSCRIPT

Page 1: [ASM]Lab7

Assembly Language (Lab 7)PROCS + New Instructions

Page 2: [ASM]Lab7

AgendaShift RotateMultiplication Division IntToBinStrBinary SearchAssignment Attendance Inshaa Allah :P

New Instructions

Hands On

Page 3: [ASM]Lab7

Bonus ***Gain bonus by searching for USES operator Understand it’s usage then spread your knowledge with

us next time

Any one did it?

Page 4: [ASM]Lab7

USES Operator Automatic PUSH and POP for PROCS registers

BinarySearch PROC USES esi edi ebx ecx, arr:PTR DWORD, count:DWORD, value:DWORD

Page 5: [ASM]Lab7

Shift

Page 6: [ASM]Lab7

Shifting InstructionsShifting means to move bits right and left inside an

operand.There are two ways to shift an operand’s bits:

1.Logical Shift 2.Arithmetic Shift

fills the newly created bit position with zero

The newly created bit position is filled with a copy of the original number’s sign bit

Page 7: [ASM]Lab7

1.Logical Shift

Page 8: [ASM]Lab7

SHL (shift left) instruction1. Performs a logical left shift on the destination operand2. Filling the lowest bit with 0. 3. The highest bit is moved to the Carry flag

The bit that was in the Carry flag is discarded

CF

0

SHL destination, countShift

Count

Page 9: [ASM]Lab7

SHL (shift left) instructionThe following lists the types of operands permitted by this instruction:

SHL reg, imm8SHL mem, imm8SHL reg, clSHL mem, cl

integer between 0 and 255

CL registercan contain a shift

count

Page 10: [ASM]Lab7

SHR (shift right) instruction1. Performs a logical right shift on the destination

operand2. Replacing the highest bit with a 0. 3. The lowest bit is copied into the Carry flag

The bit that was previously in the Carry flag is lost

Shift Count

CF

0

SHR destination, count

Page 11: [ASM]Lab7

Exercises (AL = ? & CF = ?)

11

mov al, 42shl al, 1

mov al, 10000000bshl al, 2

mov al, 26shr al, 1

mov al, 00000010bshr al, 2

; AL = 00101010; AL = 01010100 = (84)10, CF=0

; AL = 00000000b, CF = 0

; AL = 00011010; AL = 00001101 = (13)10, CF=0

; AL = 00000000b, CF = 1

Page 12: [ASM]Lab7

Did You Notice?

42

SHL

84

26

SHR

13Fast

BINARY Division

Fast BINARY Multiplication

&

Page 13: [ASM]Lab7

Fast Multiplication

SHL can perform multiplication by powers of 2. Shifting an unsigned integer left by n bits multiplies the

operand by 2n

For example, shifting 5 left by 1 bit produces 105 * 21 = 10

SHL dest, cnt

dest *= 2 cnt

mov dl, 5Shl dl, 1

0 0 0 0 0 1 0 1 5 0 0 0 0 1 0 1 0 10

Page 14: [ASM]Lab7

Fast Division

SHR can perform division by powers of 2. Shifting an unsigned integer right by n bits divides the

operand by 2n

For example, shifting the 32 left by 1 bit produces 1664 / 23 = 8

mov dl, 64Shr dl, 3

0 1 0 0 0 0 0 0 640 0 0 0 1 0 0 0 8

SHR dest, cnt

dest /= 2 cnt

Page 15: [ASM]Lab7

2.Arthimitic Shift

Page 16: [ASM]Lab7

SAL & SARSAL (shift arithmetic left) instruction Works the same as the SHL instruction, the lowest bit is

assigned to 0.

SAR (shift arithmetic right) instructionWorks the same as SHR but it preserves the number’s

sign bit

CF

0

Page 17: [ASM]Lab7

Exercises (AL = ? & CF = ?)

17

mov al, -26SAR al, 1

Mov CL, 1Mov AL, 42SAL AL, CL

SAR AL, BL

; AL = 11100110; AL = 11110011 = (-13)10, CF=0

; To use CL as shift count; AL = 00101010; AL = 01010100 = (84)10, CF=0

;Error: only CL can be used as count

Page 18: [ASM]Lab7

Fast Signed Multiplication & Division

SAL/SAR can also be used in multiplication/division by 2n with signed numbers

Page 19: [ASM]Lab7

Rotate

Page 20: [ASM]Lab7

Rotate Inst.Rotate instructions are the same as logical shift

instructions except that the lost bit from one end in shifting is inserted in the other end in rotating.

They are circular shift or simply rotation.

Page 21: [ASM]Lab7

ROL (Rotate Left) Instruction 1. Shifts each bit to the left. 2. The highest bit is copied into the Carry flag and the

lowest bit position.Bit rotation does not lose bits. A bit rotated off one end

of a number appears again at the other end

ROL dest, cnt

Page 22: [ASM]Lab7

ROR (Rotate Right) Instruction Shifts each bit to the right. The lowest bit is copied into the Carry flag and the

highest bit position.

ROR dest, cnt

Page 23: [ASM]Lab7

Exercise

23

mov al, 128rol al, 1

mov al, 00010000bRol al, 3

mov al, 01hRor al, 1

mov al, 00000100bRor al, 3

; AL = 10000000b , CF = 0; AL = 00000001b = (1)10, CF=1

; AL = 10000000b, CF = 0

; AL = 00000001b; AL = 10000000b , CF=1

; AL = 10000000b, CF = 1

Page 24: [ASM]Lab7

RCL & RCR Instruction There is another set of rotation instructions which are

RCL (rotate carry left) and RCR (rotate carry right).

They assume the carry bit is an extra bit in the operand and rotate the whole bits including the carry bit.

RCL dest, cnt

RCR dest, cnt

Page 25: [ASM]Lab7

Exercise RCL = From LEFT to CF and from CF to RIGHTRCR = From Right to CF and from CF to Left

RC? = From ? To CF and From CF to ?-1

clcmov bl, 88hrcl bl, 1rcl bl, 1

; clear carry instruction, CF = 0; BL = 10001000b ; BL = 00010000b , CF=1; BL = 00100001b , CF=0

Page 26: [ASM]Lab7

Hands OnReady … Steady … Code

Page 27: [ASM]Lab7

IntToBinStrConvert an integer entered by user into a binary stringHints:

First initialize the resultant string by zero characters Then, continuously left shift the number till it becomes zero If the carry flag after each shift is set, then change

corresponding character in the string to ‘1’

Enter an integer: 1200000000000000000000000000001100

JC jump if carry flag = 1JNC jump if carry flag = 0

Page 28: [ASM]Lab7

INCLUDE Irvine32.inc.data

promptMsg byte "Enter an integer: ", 0strResult byte 32 dup('0'), 0 ;initialize string by '0' characters

.codeIntToBinStr PROTO number:DWORD, strBin:PTR BYTE

main PROCmov edx, offset promptMsgcall writestring

call readint ; number is stored in eax

INVOKE IntToBinStr, eax, offset strResult

mov edx, offset strResult ; disaply the result call writestringcall Crlf

exitmain ENDP

Page 29: [ASM]Lab7

IntToBinStr PROC number:DWORD, strBin:PTR BYTEpush esi

mov esi, strBinl1:

shl number, 1;Left-shift number to get the most significant bit in CFjnc skip ;If CF=0, leave this char in string as '0'mov byte ptr [esi], '1' ;Otherwise, make it '1'

skip:inc esi ;Move to the next char in strBincmp number, 0

jne l1 ;if eax is zero, stop (no need to continue)

pop esiret

IntToBinStr ENDPEND main

Page 30: [ASM]Lab7

Multiplication

Page 31: [ASM]Lab7

MUL/IMUL

Only one Operand ?!!What is the difference between SHL & MUL?

MUL/IMUL r/m8 | r/m16 | r/m32

Page 32: [ASM]Lab7

What is the difference between SHL & MUL?

SHL, is concerned in multiplication by power of twos ONLY

MUL, can multiply by any number (Generic)

Page 33: [ASM]Lab7

Only one operand? … YESThe instructions take only one operand which is the

multiplierNotes: 3*51. The multiplicand is assumed to be stored in specific

register2. Also, operation results will be stored in specific

register(s) not in the given operand.The locations of the second operand (multiplicand) and

operation result differ according to the size of the given operand

multiplicandmultiplier

Page 34: [ASM]Lab7

Operand Sizes

1. The registers holding the product are twice the size of multiplicand and the multiplier, guaranteeing that overflow will never occur.

2. The product value is divided into two halves: upper and lower. The upper half is AH, DX, and EDX in case if the product value is AX, DX:AX, and EDX:EAX respectively

3. (MUL) Overflow (OF) and carry (CF) flags are set if the upper half is not equal to zero.

4. (IMUL) Overflow and carry flags are set if the upper half is not sign-extension of the lower half of product.

Page 35: [ASM]Lab7

Exercise

35

mov AL, 5mov BL, 10mul BL

mov AL, 50mov BL, 10mul BL

;AX = 0032h = (50)10, CF = 0

;AX = 01F4h = (500)10, CF = 1

Page 36: [ASM]Lab7

Exercise

mov AL, 48mov BL, 4imul BL

mov AL, -4mov BL, 4imul BL

;AX = 00C0 = (+192)10, OF = 1;Note that +192 does not fit in AL (lower half of product) as a signed integer, so OF=1

;AX = FFF0 = (-16)10, OF = 0;Note that even AH (upper half of product)is not equal to zero but it is just sign-;extension for AL, so OF=0

+192SBYTE Range+127 To -128

Page 37: [ASM]Lab7

Division

Page 38: [ASM]Lab7

DIV & IDIV

Where r/m8 means 8-bit register or memory byte, r/m16 means 16-bit register or memory word, and r/m32 means 32-bit register or memory double word.

DIV/IDIV r/m8 | r/m16 | r/m32

Page 39: [ASM]Lab7

Operand Sizes

The registers holding the quotient are half the size of dividend, assuming result value will be smaller enough to fit in the register… BUT

This assumption is not always true

Page 40: [ASM]Lab7

DIV & IDIV Quirks 1. If the quotient is too large to fit into destination

operand, a division overflow error occurs causing the current program halts!

2. If the divisor is zero, an integer divide by zero error occurs causing the current program halts!

3. A very common mistake is to forget to initialize DX or EDX before use 16-bit or 32-bit division.

Page 41: [ASM]Lab7

Key Inst.In IDIV instruction, which used in signed division,

dividend should be well initialized to preserve the sign of dividend. There are three instructions extends the sign bit in AL, AX, and EAX to AX, DX, and EDX respectively

Page 42: [ASM]Lab7

Exercise

MOV AX, 82 MOV BL, 4DIV BL

MOV DX, 0 MOV AX, 82MOV BX, 4DIV BX

;Even 82 fit in AL, you should MOV it in AX to initialize AH too;AL (quotient) = 20, ;AH (remainder) = 2

;As we use 16-bit division, we should initialize DX too;AX (quotient) = 20, ;DX (remainder) = 2

Page 43: [ASM]Lab7

Exercise

MOV AL, -100CBW MOV BL, 2IDIV BL

MOV AX, -100 MOV BL, 2IDIV BL

;We have to extend the sign to AX;AL(quotient)= CEh = -50, ;AH (remainder) = 0

;Instead of use of CBW, move -100 to AX directly;AL(quotient)= CEh = -50,;AH (remainder) = 0

Page 44: [ASM]Lab7

Exercise

MOV AX, 200 MOV BX, 4CWD IDIV BX

MOV AX, 1000HMOV BL, 10HDIV BL

;AX = 00C8h, DX = ?;DX:AX = 0000h:00C8h;AX (quotient) = 50,;DX (remainder) = 0

;ERROR: Integer overflow (100h doesn’t fit in AL)

Page 45: [ASM]Lab7

Binary Search Algorithm

Page 46: [ASM]Lab7

Binary Search AlgorithmPrerequisite: Sorted Array (Assume Ascending)Receives: Search For “value”Returns: The index of the value or -1 if not exists

Page 47: [ASM]Lab7

int BSearch(int values[], int count, int SearchVal){

int first = 0;int last = count - 1;while (first <= last){

int mid = (last + first) / 2;if (values[mid] < SearchVal)

first = mid + 1;else if (values[mid] > SearchVal)

last = mid - 1;else

return mid;}return -1;

}

Page 48: [ASM]Lab7

The SolutionHow to Write in ASSEMBLY !!

Page 49: [ASM]Lab7

.datas1 BYTE "Enter a value to search for: ",0s2 BYTE "This value in index: ",0arr1 DWORD 10, 20, 30, 40, 50

.codeBinarySearch PROC USES esi edi ebx ecx, arr:PTR DWORD, count:DWORD, value:DWORD

mov esi, 0;ESI: first index (initially = 0)mov edi, countdec edi ;EDI: last index (initially = count -1)

mov ecx, value ;ECX: the search value mov eax, arr ;EAX: pointer for the array, actual address

.while esi <= edimov ebx, esiadd ebx, edishr ebx, 1 ;EBX: mid index = (first + last)/2

Page 50: [ASM]Lab7

;[eax+ebx*4] is the current mid value

.IF [eax+ebx*4] < ecx mov esi, ebx ;first = mid + 1inc esi

.elseif [eax+ebx*4] > ecxmov edi, ebx ;last = mid - 1dec edi

.elsemov eax, ebx ;Found: EAX = mid index jmp return

.endif.endw

mov eax, -1;Not Found: EAX = -1

return:retBinarySearch ENDP

Page 51: [ASM]Lab7

main procmov edx, offset s1call writestring

call readdec

INVOKE BinarySearch, offset arr1, lengthof arr1, eax

mov edx, offset s2call writestringcall writeIntcall CrLf

exitmain endpEND main

Page 52: [ASM]Lab7

AssignmentYou Must Use PROC in Your Solution…

Page 53: [ASM]Lab7

IsPrime?Write an assembly program that checks if the input

number is prime or not.

Page 54: [ASM]Lab7

XY

Write an assembly program that calculates the power of two integers (xy) using repetitive multiplications.

Page 55: [ASM]Lab7

Questions?!

Page 56: [ASM]Lab7

Thanks!