Computer Organization and Assembly Language: Lecture 6 - Conditional Processing
Computer Organization and Assembly Language: Lecture 6 - Conditional Processing
Computer Organization and Assembly Language: Lecture 6 - Conditional Processing
Assembly Language
Lecture 6 - Conditional Processing
x y x ∧y
0 0 0
0 1 0
1 0 0
1 1 1
AND Instruction
• The AND instruction performs a bit wise AND
operation between corresponding bits in the two
operands and places the result in the first operand.
• The format for the AND instruction is:
AND reg, reg
AND reg, mem
AND reg, immed
AND mem, reg
AND mem, immed
reg, mem, and immed can be 8, 16, or 32
bits.
AND Instruction (continued)
• An example of ANDing:
00111011
cleared 00001111 unchanged
00001011
• The AND instruction can be used to clear selected
bits in an operand while preserving the remaining
bits. This is called masking.
mov al, 00111011b
and al, 00001111b ; AL = 00001011b
.data
array BYTE 50 DUP(?)
.code
mov ecx, LENGTHOF array
mov esi, OFFSET array
L1:
and byte ptr [esi], 11011111b
inc esi
loop L1
OR Operation
x y x ∨y
0 0 0
0 1 1
1 0 1
1 1 1
XOR Operation
x y x⊕y
0 0 0
0 1 1
1 0 1
1 1 0
OR Instruction
• The OR instruction performs a bit wise OR
operation between corresponding bits in the two
operands and places the result in the first operand.
• The format for the OR instruction is:
OR reg, reg
OR reg, mem
OR reg, immed
OR mem, reg
OR mem, immed
reg, mem, and immed can be 8, 16, or 32
bits.
OR Instruction (continued)
• An example of ORing:
unchanged 00111011 set
00001111
00111111
• The OR instruction can be used to set selected bits
in an operand while preserving the remaining bits.
mov al, 00111011b
or al, 00001111b ; AL = 001111111b
OR: Some Examples
• OR can be used to convert a one-digit value into
its ASCII equivalent:
mov dl, 5 ; binary value
or dl, 30h ; convert to ASCII
• ORing a value with itself preserves the value but
sets to flags
– ZF = 1 if AL = 0
– SF=1 if AL < 0
– SF = ZF = 0 if AL > 0
or al, al ; sets the flags
XOR Operation
x y x⊕y (x ⊕ y) ⊕ y
0 0 0 0
0 1 1 0
1 0 1 1
1 1 0 1
XOR Instruction
• The XOR instruction performs a bit wise
Exclusive OR operation between corresponding
bits in the two operands and places the result in
the first operand.
• The format for the XOR instruction is:
XOR reg, reg
XOR reg, mem
XOR reg, immed
XOR mem, reg
XOR mem, immed
reg, mem, and immed can be 8, 16, or 32
bits.
NOT Instruction
• The NOT instruction reverse all bits in an
operand:
NOT reg
NOT mem
• Example:
mov al, 11110000b
not al ; AL = 0Fh
TEST Instruction
• The TEST instruction performs an implied AND
operation between corresponding bits in the two
operands and sets the flags without modifying
either operand.
• The format for the TEST instruction is:
TEST reg, reg
TEST reg, mem
TEST reg, immed
TEST mem, reg
TEST mem, immed
reg, mem, and immed can be 8, 16, or 32
bits.
0 0 1 0 0 1 0 1 ← input value
0 0 0 0 1 1 0 1 ← test value
0 0 0 0 0 0 0 1 ← result: ZF = 0
0 0 1 0 0 0 1 0 ← input value
0 0 0 0 1 1 0 1 ← test value
0 0 0 0 0 0 0 0 ← result: ZF = 1
CMP Instruction
CMP Results
CMP Results ZF CF
destination = source 1 0
CMP Results
destination = source ZF = 1
mov ax, 5
cmp ax, 6
jl L1 ; jump if less
mov ax, 5
cmp ax, 4 ; jump if greater
JCXZ Jump if CX = 0 CX = 0
JECXZ Jump if ECX = 0 ECX = 0
JB Jump if below CF = 1
(op1 < op2)
JNAE Jump if not above CF = 1
JS Jump if signed SF = 1
(op1 is negative)
JNS Jump if not signed SF = 0
JO Jump if overflow OF = 1
JNO Jump if not OF = 0
overflow
.data
intArray SWORD 0, 0, 1, 20, 35, -12, 66, 4, 0
noneMsg BYTE "A nonzero value wasnt found", 0
.code
main PROC
mov ebx, OFFSET intArray
; point to the array
mov ecx, LENGTHOF intArray
; loop counter
found:
movsx eax, word ptr [ebx]
call WriteInt
jmp quit
INCLUDE Irvine32.inc
KEY = 239 ; Any value Between 1-255
BUFMAX = 128; Maximum buffer size
.data
sPrompt BYTE "Enter the plain text:
", 0
sEncrypt BYTE "Cypher text: ", 0
sDeCrypt BYTE "Decrypted: ", 0
buffer BYTE BUFMAX dup(0)
bufSize DWORD ?
.code
main PROC
call InputTheString
; input the plain text
call TranslateBuffer
; encrypt the buffer
mov edx, OFFSET sEncrypt
; display encrypted message
call DisplayMessage
call TranslateBuffer
; decrypt the buffer
mov edx, OFFSET sDecrypt
; display decrypted message
call DisplayMessage
exit
main ENDP
;-------------------------------------------
InputTheString PROC
;
; Asks the user to enter a string from the
; keyboard. Saves the string and its length
; in variables
; Receives: nothing
; Returns: nothing
;-------------------------------------------
pushad
mov edx, OFFSET sPrompt ; display prompt
callWriteString
mov ecx, BUFMAX ; maximum character count
mov edx, OFFSET buffer ; point to the buffer
callReadString ; input the string
mov bufsize, eax ; save the length
callCrLf
popad
ret
InputTheString ENDP
;----------------------------------------
DisplayMessage PROC
;
; Displays the encrypted or decrypted
; message
; in variables
; Receives: EDX points to the message
; Returns: nothing
;----------------------------------------
pushad
callWriteString
mov edx, OFFSET buffer ; display the buffer
callWriteString
callCrLF
callCrLf
popad
ret
DisplayMessage ENDP
;----------------------------------------
TranslateBuffer PROC
;
; Translate the sring by exclusive-ORing
; each byte with the same integer
; Receives: nothing
; Returns: nothing
;----------------------------------------
pushad
mov ecx, bufSize ; loop counter
mov esi, 0 ; index 0 in buffer
L1:
xor buffer[esi], KEY; translate a byte
inc esi ; point to next byte
loopL1
popad
ret
TranslateBuffer ENDP
END main
LOOPZ and LOOPE Instructions
• LOOPZ (Loop if zero) and LOOPE (Loop if
equal) let a loop continue if ZF = 1 & CX >
0 (First CX is decremented)
• The syntax is:
LOOPZ destination
LOOPE destination
LOOPNZ - an Example
INCLUDE Irvine32.inc
.data
array SWORD -3, -6, -1, -10, 10, 30, 40, 4
Msg BYTE " is a positive value", 0
sentine SWORD 0
.code
main PROC
mov esi, OFFSET array
mov ecx, LENGTHOF array
next:
test WORD PTR [esi], 8000h ; test sign bit
pushfd ; push flags on stack
add esi, TYPE array
popfd ; pop flags
loopnz next ; continue loop
jnz quit ; none found
sub esi, TYPE array ; ESI points to value
quit:
movzx eax, word ptr [esi] ; print value
call WriteDec
mov edx, OFFSET Msg
call WriteString
exit
main ENDP
END main
Writing IF-THEN
In C++: is In Assembler
if (x > y) x>y mov ax, x
z = 0; ? cmp ax, y
yes jng L1
no mov ax, 0
z=0 mov z, ax
L1:
Writing IF-THEN-ELSE
In C++: In Assembler
if (x > y) is mov ax, x
z = x;
x>y cmp ax, y
?
else jng L1
yes no
z = y; mov ax, x
jmp L2
z=x z=x
L1:
mov ax, y
L2:
mov z, ax
x = x + 3;
x>y mov ax, x
?
cmp ax, y
yes no
jg L2
mov ax, x
x = x+3 add x, 3
jmp L1
L2: