PHY4635/5635 Spring 2009 Lecture 8: Program Control Instructions
Short, Near and Far Jumps Short jump: jump is within +127 to -128 bytes from the address following the jump. Relative jumps : moves with the code since it stores only the relative distance within the instruction (relocatable). JMP $+2 ; jumps over the next two memory locations JMP SHORT LABEL ; the SHORT is not necessary. Near jump: jump is within ±32K bytes (anywhere in the current code segment) from the address following the jump (segments are cyclic). Note, the range is ±2GB for the 80386+ Also Relative (relocatable) Uses a sign-extended 16-bit displacement (808x) or 32-bit (80386+) JMP NEAR PTR LABEL ; the NEAR is not necessary Far jump: jump to anywhere in the real mode (intersegment jump) Obtains a new segment and offset (not relative). JMP FAR PRT LABEL You need not code a particular type of jump the assembler will do it for you.
Short, Near and Far Jumps Far jump to an external: EXTRN UP:FAR ; Defines a FAR label ( ) START: XOR BX,BX MOV AX,1 ADD AX,BX JMP NEAR PTR NEXT ; NEAR PTR not necessary ( ) NEXT: MOV BX,AX JMP START JMP FAR PTR UP ( ) Inside the routine that contains UP label ( ) MOV AX,FFh UP:: MOV BX,1 ; Note the use of :: to indicate that it may be ; accessed by other routines as an EXTRN. ( ) Linker inserts the address of the UP label into the JMP UP instruction for you. It also inserts the segment address of the JMP START instruction
Indirect Jumps Register holds the jump address Allows you to select where to jump based on a table of labels.
Indirect Jumps Register holds the jump address Allows you to select where to jump based on a table of labels.
SET Either set a byte (01h) or clear a byte (00h) after checking a condition. Nice to store the state of the machine into memory at one point in the code for use later. SETNC MEMLOC ;Sets MEMLOC to 1 if C-bit=0
LOOP LOOP: Combines DEC CX with JNZ. LOOP LABEL ;LABEL is near Similar to REP with string instructions which decrements CX and stops loop when CX=0. LOOPD (ECX) and LOOPW (CX) in 80386+ Other forms (combines C and Z-flags): LOOPE (Loop while equal) (same as LOOPZ): Loops if a condition is equal (Z=1) AND CX 0 LOOPNE (Loop while not equal) (same as LOOPNZ): Loops if a condition is not equal (Z=0) AND CX 0.
The STACK Temporary holding space for WORD data Usually do not need to reference specific stack memory, but FYI SS:SP Points to top of stack play with SP to index down into the stack. Be careful all data is 1 word in length! All segments, including stack, rolls over at boundaries PUSH places a word of data on top of the stack (never a byte) POP pulls a word of data memory off the top of the stack (never a byte) LIFO (Last In, First Out)
PUSH AX The Stack
POP BX The Stack
PUSHA (not recognized in WinASM) The Stack
Procedures (subroutines) CALL pushes the IP onto the stack (location of next sequential instruction) for use when returning (RET) from procedure. RET - Used within a procedure, pops the IP back for continued program flow CLRSCRN PROC NEAR ;PROC directive labels it as a proc ( ) RET CLRSCRN ENDP
Procedures (subroutines) Near CALL (intrasegment) are used locally PUSHes IP 3-bytes long (OP and ±32K distance added to the IP), same as near jump. Far CALL (intersegment) are used globally PUSHes CS AND IP 5-bytes long (must contain complete address can point to any memory location) Near or Far determined in PROC statement during assembly and link.
CALLs with register operands ( ) ( ) DISP DISP ( ) ;Write OK to screen MOV BX, OFFSET DISP MOV DL, O CALL BX ;BX holds DISP offset. MOV DL, K CALL BX PROC NEAR MOV AH,2 INT 21h RET ENDP
CALLs with indirect memory address Useful when different subroutines need to be chosen based on conditions
Example from skeleton.asm ;==============subroutine CLS=============== ;clear video screen by getting and setting video mode ;uses BIOS INT 10H ;inputs -- none ;outputs -- none ;destroys -- nuthin' ;----------------------------------------------------------------- cls proc near pushall ;uses pushall macro sub bh,bh mov ah,0fh int 10h sub ah,ah int 10h popall ;uses popall macro Ret cls EndP
Example from skeleton.asm ;==============subroutine CLS=============== ;clear video screen by getting and setting video mode ;uses BIOS INT 10H ;inputs -- none ;outputs -- none ;destroys -- nuthin' ;----------------------------------------------------------------- cls proc near call pushall ;uses pushall procedure sub bh,bh mov ah,0fh int 10h sub ah,ah int 10h call popall ;uses popall procedure Ret cls EndP
PUSHALL/POPALL PROCEDURES ;====subroutine PUSHALL===== ;push all registers to stack ;uses nuthin ;inputs -- none ;outputs -- none ;destroys -- nuthin' ;-------------------------------------------- pushall proc near push ax push bx push cx push dx Ret cls EndP ;====subroutine POPALL===== ;push all registers to stack ;uses nuthin ;inputs -- none ;outputs -- none ;destroys -- nuthin' ;-------------------------------------------- pushall proc near pop dx pop cx pop bx pop ax Ret cls EndP