Jimp

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Assembler Details, CONDITIONAL

JUMP &LOOP Instructions


Dr. REKHA.K.S.
Associate Professor,
Dept of CS&E,
NIE,MYSURU
4–7 ASSEMBLER DETAIL
• The assembler can be used two ways:
– with models unique to a particular assembler
– with full-segment definitions that allow complete
control over the assembly process and are universal
to all assemblers
• In most cases, the inline assembler found in
Visual is used for developing assembly code for
use in a program
– occasions require separate assembly modules using
the assembler
Directives
• Indicate how an operand or section of a
program is to be processed by the assembler.
– some generate and store information in the
memory; others do not
• The DB (define byte) directive stores bytes of
data in the memory.
• BYTE PTR indicates the size of the data
referenced by a pointer or index register.
Storing Data in a Memory Segment
• DB (define byte), DW (define word), and DD
(define doubleword) are most often used with
MASM to define and store memory data.
• If a numeric coprocessor executes software in
the system, the DQ (define quadword) and DT
(define ten bytes) directives are also common.
• These directives label a memory location with
a symbolic name and indicate its size
• Memory is reserved for use in the future by
using a question mark (?) as an operand for a
DB, DW, or DD directive.
– when ? is used in place of a numeric or ASCII
value, the assembler sets aside a location and
does not initialize it to any specific value
– ARR DB 5 DUP (?)
– ARR DB 5 DUP(2)
– Outputs 5 bytes that all have the value 2.
PROC and ENDP
• Indicate start and end of a procedure
(subroutine).
– they force structure because the procedure is
clearly defined
• If structure is to be violated for whatever
reason, use the CALLF, CALLN, RETF, and RETN
instructions.
• Both the PROC and ENDP directives require a
label to indicate the name of the procedure.
• The PROC directive, which indicates the start of
a procedure, must also be followed with a
NEAR or FAR.
– A NEAR procedure is one that resides in the same
code segment as the program, often considered to
be local
– A FAR procedure may reside at any location in the
memory system, considered global
• The term global denotes a procedure that can
be used by any program.
• Local defines a procedure that is only used by
the current program.
• DELAY PROC
• MOV AX,02FFH : outer counter
• B2: MOV CX,0FFFFH; inner counter
• B1: LOOP B1 ; decrement inner count till CX=0
• DEC AX; decrement outer count till AX=0
• JNZ B2
• RET
• DELAY ENDP
• END
CMP Instruction
• CMP CX,BX
CF ZF SF
CX=BX 0 1 0
CX>BX 0 0 0
CX<BX 1 0 1

CMP AX,4372H
JB label 1

Label1: __________
Branch Instructions
Jump Instructions are used for changing the flow of execution
of instructions in the processor. If we want jump to any
instruction in between the code, then this can be achieved by
these instructions. There are two types of Jump instructions:

• Conditional Jump/Branch instructions


• Unconditional Jump/Branch Instructions
Unconditional Jump Instructions

• These instructions are used to jump on a particular location unconditionally,


i.e. there is no need to satisfy any condition for the jump to take place. There
are three types of procedures used for unconditional jump. They are:
• NEAR – This procedure targets within the same code segment. (Intra-
segment)
• SHORT - This procedure also targets within the same code segment, but the
offset is 1 byte long. (Intra-segment)

• FAR - In this procedure, the target is outside the segment and the size of the
pointer is double word. (Inter-segment)

• Syntax: JMP procedure_namememory_location

• Example: JMP short


Conditional Branch Instructions
• 2 byte Machine code
• OPCODE DB D8 IP+D8 or IP-D8
• 0050 Again: INC CX
• 0052 ADD AX,[BX]
• 0054 JNS AGAIN
• 0056 NEXT: MOV Result,CX
• Effective Branch Address = 0050
• - 0056
- 6
• -------------
Types
JZ/JE (jump on ZERO/JUMP on EQUAL)
• Transfer will be within -128 to +127
Format : JZ OPR

CMP BX,DX
JE DONE ; if true ZF=1
SUB BX,AX
INC CX
DONE: MOV AX,CX
JNZ/JNE(Branch on Non Zero/Branch on
Not EQUAL)
JNZ OPR

MOV BX,2374H
NXT : ADD AX,0002H
DEC BX
JNZ NXT
JS(Branch on Sign Bit)
ADD BL,DH
JS print ;SF=1

JNS(Branch on Sign clear) SF=0


DEC AL
JNS PRINT

PRINT: ________
JO (Branch on overflow) OF =1
ADD BL,BL
JO Error
MOV SUM,AL

• Error: ________
JNO OPR(Branch on NO OVERFLOW) OF=0
JPE/JP (Branch on Parity/Branch on even parity) PF=1
JP OPR
IN AL,F8H
OR AL,AL
JPE Error

Error:_________

• +127
• +127 using 8 bit register =254
• 1111(binary)- 2 ( in 2’scomplement)
• Negative result out of positive operand is overflow
128 64 32 16 8 4 2 1
1 1 1 1 1 1 0
1s complement 0 0 0 0 0 0 1
2s complement 1
0 0 0 00 010
JB/JNAE/JC(Branch on below/not above/not EQUAL)
• CF=1

JB OPR
• CMP CX,BX
• CF ZF SF
• CX=BX 0 1 0
• CX>BX 0 0 0
• CX<BX 1 0 1
• CMP AX,4372H
• JB label 1

• Label1: __________
JNB/JAE/JNC (CF=0)

JBE/JNA (Branch on below or equal or not


above)
• CF=1 ZF=1

JL/JNGE(Branch on less, or not greater or not


equal)
• If sign flag != overflow flag
JNL/JGE( Jump on not less/Jump on greater)
• SF=OF

JLE/JNG (Jump if less than or equal/Jump if not


greater)
• ZF=1
• SF !=OF

JNLE/JG
• CF=OF
LOOP instruction
• Number of count loaded in CX
• CX automatically decremented
• If CX != 0 execution jump to label
• If CX= 0 next instruction executed
• MOV BX,OFFSET ARRAY
• MOV CX,04
• NEXT : MOV AL,[BX]
• ADD AL,07H
• DAA
• MOV [BX},AL
• INC BX
• LOOP NEXT
LOOPZ/LOOPE(LOOP WHILE ZERO OR EQUAL)
• LOOPZ
• Repeat until ZERO FLAG =0
• If CX !=0 & ZF =1 execution jump to label
specified
• IF CX = 0 or ZF=0 execution go to next instruction
• MOV BX,OFFSET ARRAY
• MOV CX,08
• NEXT: INC BX
• CMP[BX],0FFH
• LOOPE NEXT
LOOPNZ/LOOPNE(LOOP WHILE NON ZERO
OR NOT EQUAL
• ZF=0 & CX !=0 🡪 EXECUTION GOES TO LABEL

• IF CX=0 & ZF=1 -> EXECUTION GOES TO NEXT


INSTRUCTION
JCXZ
• Jump to label if CX register =0
• If CX != 0 -> next instruction

• JCXZ SKIP_LOOP
• NEXT : SUB[BX],07H
• INC BX
• LOOP NEXT
• SKIP_LOOP:________
NOP(NO OPERATION PERFORMED)
• It is used to insert delay
• Uses 3 clock cyles
• No flags affected
• Delay can be incremented
• Ex : MOV CX,N ;4
Back : NOP ;3
• NOP ;3
• Loop Back
Memory Organization
• The assembler uses two basic formats for
developing software:
– one method uses models; the other uses full-
segment definitions
• Memory models are unique to MASM.
• The models are easier to use for simple tasks.
• The full-segment definitions offer better
control over the assembly language task and
are recommended for complex programs
Full-Segment Definitions
• Full-segment definitions are also used with
the Borland and Microsoft environments for
procedures developed in assembly language.
• The names of the segments in this program
can be changed to any name.
• Always include the group name ‘DATA’, so the
Microsoft program CodeView can be used to
symbolically debug this software.
• data segment
• a dw 9A88h
• b dw 8765h
• c dw ?
• data ends

• code segment
• assume cs:code,ds:data
• start:
• mov ax,data
• mov ds,ax
• mov ax,a
• mov bx,b
• sub ax,bx
• mov c,ax
• Mov ah,4ch
• Int 21h
• code ends
• end start
Models
• There are many models available to the MASM
assembler, ranging from tiny to huge.
• Special directives such as @DATA are used to identify
various segments.
• Models are important with both Microsoft Visual and
Borland development systems if assembly language is
included with programs
.TINY - Code & data segment <=64K
.SMALL – Code<=64k, Data <=64K
.MEDIUM- Data <=64k, Code any size
.Model small ; is used when DS<=64 K, S<=64K
.Data
Msg db “be good$”
.Code
Mov ax,@data
Mov ds,ax
Lea dx, msg
Mov ah,9 ; display a message
Int 21h
Mov ah,4ch; exit
Int 21h
END

You might also like