UNCONDITIONAL JUMP

Posted by Atezaz | 6:45 PM | | 0 comments »

Till now we have been placing data at the end of code. There is no such restriction and we can define data anywhere in the code. Taking the previous example, if we place data at the start of code inste ad of at the end and we load our program in the debugger. We can see our data placed at the start but the debugger is intending to start execution at our data. The COM file definition said that the first executable instruction is at offset 0100 but we have placed data there instead of code. So the debugger will try to interpret that data as code and showed whatever it could make up out of those opcodes.

We introduce a new instruction called JMP. It is the unconditional jump that executes regardless of the state of all flags. So we write an unconditional jump as the very first instruction of our program and jump to the next instruction that follows our data declarations. This time 0100 contains a valid first instruction of our program.

Example 3.2

001 ; a program to add ten numbers without a separate counter
002 [org 0x0100]
003 jmp start ; unconditionally jump over data
004
005 num1: dw 10, 20, 30, 40, 50, 10, 20, 30, 40, 50
006 total: dw 0


41


007

008 start: mov bx, 0 ; initialize array index to zero
009 mov ax, 0 ; initialize sum to zero
010
011 l1: add ax, [num1+bx] ; add number to ax
012 add bx, 2 ; advance bx to next index
013 cmp bx, 20 ; are we beyond the last index
014 jne l1 ; if not add next number
015
016 mov [total], ax ; write back sum in memory
017
018 mov ax, 0x4c00 ; terminate program
019 int 0x21

003 JMP jumps over the data declarations to the start label and

execution resumes from there.

0 comments