CEL-324 Computer Organization and Assembly Language Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

CEL-324

Computer Organization

and Assembly Language Lab

Department of Computer Science

Bahria University, Islamabad


Lab # 06: Conditional Jumps and Menu driven Procedures
Objectives:

Develop procedures and use them to create menu driven programs. All questions will have menus user
will choose the options and programs should process accordingly.

Tools Used: Microsoft Visual Studio

Tools Used:

Visual Studio

Specific Flag values

Status Flags

The status flags reflect the outcomes of arithmetic and logical operations performed by the CPU. They
are Overflow, sign, zero, auxiliary carry, carry, parity.

Carry Flag (CF) is set when the result of unsigned arithmetic operation is too large to fit into the
destination.

Overflow Flag (OF) is set when the result of a signed arithmetic operation is too wide to fit into
destination.

Range of 8-, 16-, and 32-bit signed numbers


Size Range
8 bits − 128 to +127
16 bits − 32,768 to +32,767
32 bits −2,147,483,648 to +2,147,483,647

Sign Flag (SF) is set when the result of an arithmetic operation generates negative result. High-order bit
of result.

1 -- Negative signed number.


0 -- positive signed number.

Zero Flag (ZF) is set when the result of arithmetic operation generates a result of zero.

Auxiliary carry Flag is set when an arithmetic operation causes a carry from bit 3 to bit 4 in an 8-bit
operand.
Parity Flag is set when sums the result of bits that are set in a number and indicate whether the sum is
even or odd. Count the number of 1 in binary if it’s odd then PF=0 otherwise its 1.

Example:

Main proc
mov al,255
add al,1
call DumpRegs ; you can also use Debugger to check FLAGS

exit
main endp
In above example

CF=1 ZF=1 AF=1 SF=0 OF =0 PF=1

Following table shows a list of jumps based on specific CPU flags values.

Mnemonics Description Flags

JZ Jump if zero ZF=1

JNZ Jump if not zero ZF=0

JC Jump if carry CF=1

JNC Jump if not carry CF=0

JO Jump if Overflow OF=1

JNO Jump if not overflow OF=0

JS Jump if sign SF=1

JNS Jump if not sign SF=0

JP Jump if parity(even) PF=1

JNP Jump if not parity PF=0


Comparisons of unsigned operands

Jumps based specifically on comparisons of unsigned integer shown in following table

Mnemonic Description

JA Jump if above (if leftOp>rightOp)

JNBE Jump if not below or equal (same as


JA)

JAE Jump if above or equal (if


leftOp>=rightOp)

JNB Jump if not below (same as JAE)

JB Jump if below (if leftOp<rightOp)

JNAE Jump if not above or equal (same as


JB)

JBE Jump if below or equal (if


leftOp_<=rightOp)

JNA Jump if not above (same as JBE)

CMP: The compare instruction performs an implied subtraction of a source operand from the destination
operand. Neither operand is modified. The CMP instruction changes the flag status.

CMP Result ZF CF

Destination<source 0 1

Destination>source 0 0

Destination=source 1 0
Comparisons of signed operands:

Mnemonic Description

JG Jump if greater (if leftOp>rightOp)

JNLE Jump if not less or equal (same as


JG)

JGE Jump if greater than or equal (if


leftOp>=rightOp)

JNL Jump if not less (same as JGE)

JL Jump if less (if leftOp<rightOp)

JNGE Jump if not greater than or equal


(same as JL)

JLE Jump if less than or equal (if


leftOp_<=rightOp)

JNG Jump if not greater (same as JLE)

When we are dealing with signed number than CMP Instruction change the sign, Zero, overflow flag
status.

CMP Result Flags

Destination<source SF≠OF

Destination>source SF=OF

Destination=source ZF=1

Equality comparisons:

These jump instructions based on evaluating either the equality of two operands or the value of cx,ecx.

Syntax: CMP leftop ,rightop


Mnemonics Description

JE Jump if equal(leftop=rightop)

JNE Jump if not equal(leftop≠rightop)

JCXZ Jump if cx=0

JECXZ Jump if ecx=0

dec:

dec instruction is used for decrementing an operand by one. It works on a single operand that can be

either in a register or in memory.

inc:

inc instruction is used for incrementing an operand by one .It works on a single operand that can be

either in a register or in memory.

ret:

used to pops the return address off the stack and returns control to that location
Lab Tasks:

1. Write a program that asks the user to enter number between 0 and 100. The program should
display the appropriate grade using Cmp and jmp.

Hint: call grade-> to print appropriate grade


grade proc
cmp eax, 100
JA gEr
ret-> used to pops the return address off the stack and returns control to that location

2. Write a program that gets a string from the user and stores the string in a buffer. Then
scan the string and separate all small alphabets in another buffer. Then print the
buffer.
Hint:
cmp al, 13
JE En
mov [esi], al
inc esi
call scan
scan PROC
mov al,[esi]
cmp al,'a'
JB nope
cmp al, 'z'
JA nope
3. Write two procedures isCapital and isSmall which return true/ false by setting/
clearing carry flag. Use these procedures to separate mixed alphabetic string into two
strings. One containing small letters and other containing capital letters. Display the
result.
Hint:
sa byte 20 DUP(?)
ca byte 20 DUP(?)
inp byte 20 DUP(?)
InpL:
call readchar
call writechar
cmp al,13
JE En
mainL:
mov al,[edx]
clc call isSmall
JC small call isCapital
4. Write procedures IsAlpha and IsDigit. Get character form the user and check whether
that character is alphabet or digit. Use carry flag to return true or false value. Print the
appropriate message.
Hint:
clc
call isaAlpha
jc alpha
call isaDigit
jc digit
5. Write a procedure that is passed two strings and compares them if they are of same
length or not.
Hint:
cmp eax,ebx
jne ex
mov edx, offset msgyes
6. Write a procedure that checks the string if it is palindrome or not.
Hint:
mov al,[esi+edx]
cmp al,[esi+ebx]
jne nope

You might also like