Upload
nora-youssef
View
281
Download
0
Embed Size (px)
Citation preview
Assembly Language (Lab 7)PROCS + New Instructions
AgendaShift RotateMultiplication Division IntToBinStrBinary SearchAssignment Attendance Inshaa Allah :P
New Instructions
Hands On
Bonus ***Gain bonus by searching for USES operator Understand it’s usage then spread your knowledge with
us next time
Any one did it?
USES Operator Automatic PUSH and POP for PROCS registers
BinarySearch PROC USES esi edi ebx ecx, arr:PTR DWORD, count:DWORD, value:DWORD
Shift
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
1.Logical Shift
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
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
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
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
Did You Notice?
42
SHL
84
26
SHR
13Fast
BINARY Division
Fast BINARY Multiplication
&
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
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
2.Arthimitic Shift
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
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
Fast Signed Multiplication & Division
SAL/SAR can also be used in multiplication/division by 2n with signed numbers
Rotate
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.
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
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
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
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
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
Hands OnReady … Steady … Code
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
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
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
Multiplication
MUL/IMUL
Only one Operand ?!!What is the difference between SHL & MUL?
MUL/IMUL r/m8 | r/m16 | r/m32
What is the difference between SHL & MUL?
SHL, is concerned in multiplication by power of twos ONLY
MUL, can multiply by any number (Generic)
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
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.
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
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
Division
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
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
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.
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
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
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
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)
Binary Search Algorithm
Binary Search AlgorithmPrerequisite: Sorted Array (Assume Ascending)Receives: Search For “value”Returns: The index of the value or -1 if not exists
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;
}
The SolutionHow to Write in ASSEMBLY !!
.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
;[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
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
AssignmentYou Must Use PROC in Your Solution…
IsPrime?Write an assembly program that checks if the input
number is prime or not.
XY
Write an assembly program that calculates the power of two integers (xy) using repetitive multiplications.
Questions?!
Thanks!