# Experiment 3 transfer instruction jump principle and its simple application programming

Source code:

```assume cs:code, ds:data

data segment
x db 1, 9, 3
len1 equ \$ - x

y dw 1, 9, 3
len2 equ \$ - y
data ends

code segment
start:
mov ax, data
mov ds, ax

mov si, offset x
mov cx, len1
mov ah, 2
s1:mov dl, [si]
or dl, 30h
int 21h

mov dl, ' '
int 21h

inc si
loop s1

mov ah, 2
mov dl, 0ah
int 21h

mov si, offset y
mov cx, len2/2
mov ah, 2
s2:mov dx, [si]
or dl, 30h
int 21h

mov dl, ' '
int 21h

loop s2

mov ah, 4ch
int 21h
code ends
end start```

Operation results: ① line27, when the assembly instruction loop s1 jumps, it jumps according to the displacement. Check the machine code through debug disassembly and analyze the jump displacement? (the displacement value is answered in decimal) from the perspective of the CPU, explain how to calculate the offset address of the instruction after the jump label s1.

In this command, the displacement is F2; Since it is in complement form, converting it to decimal number should be - 14.

First, the offset address of the instruction is 0019. According to the instruction length of the instruction, the CPU calculates that the offset address of the next instruction is 001B, and then subtracts the displacement from the offset address of the next instruction to obtain the jump offset address 000D.

② line44. When the assembly instruction loop s2 jumps, it jumps according to the displacement. Check the machine code through debug disassembly and analyze the jump displacement? (the displacement value is answered in decimal) from the perspective of the CPU, explain how to calculate the offset address of the instruction after the jump label s2.

In this command, the displacement is F0; Since it is a complement, converting it to decimal should be - 16.

First, the offset address of the instruction is 0037. According to the instruction length of the instruction, the CPU calculates that the offset address of the next instruction is 0039, and then subtracts the displacement from the offset address of the next instruction to obtain the jump offset address 0029.

③ Attach the disassembly screenshot of debugging observation in debug during the above analysis  Source code:

```assume cs:code, ds:data

data segment
dw 200h, 0h, 230h, 0h
data ends

stack segment
db 16 dup(0)
stack ends

code segment
start:
mov ax, data
mov ds, ax

mov word ptr ds:, offset s1
mov word ptr ds:, offset s2
mov ds:, cs

mov ax, stack
mov ss, ax
mov sp, 16

call word ptr ds:
s1: pop ax

call dword ptr ds:
s2: pop bx
pop cx

mov ah, 4ch
int 21h
code ends
end start```

After analysis, debugging and verification, register (ax) =? (bx) = (cx) =? Attach the screenshot of the debugging result interface.

After the call is executed, the next instruction address of the current address will be implicitly stored in the stack;

The first call stores the offset address of s1 and assigns the address to AX through pop ax, so ax=offset s1=0021h

The second call successively stores the segment address and offset address of s2. First pop up the offset address through pop bx, so bx=offset s2=0026h; then pop cx pops up the segment address, so cx=code=076c

ax=0021，bx=0026，cx=076c  Source code

```assume cs:code,ds:data
data segment
x db 99, 72, 85, 63, 89, 97, 55
len equ \$- x
data ends

code segment
start:
mov ax,data
mov ds,ax
mov si,offset x
mov cx,len

s:    call printNum
call  printSpace
inc si
loop s

mov ax,4c00h
int 21h

printNum:     mov ah,0 ;High position 0
mov al,[si] ;Number of bytes saved in low order
;Because only one character can be output at a time
;Therefore, divide the ten digits by 10 and output the quotient before outputting the remainder
mov bl,10
div bl
mov bx,ax

mov ah,2
mov dl,bl ;The lower order stores the quotient, that is, ten digits
or dl,30h ;Converting numbers to characters is equivalent to adding 48
int 21h
mov ah,2
mov dl,bh ;The high bit holds the remainder, that is, one bit
or dl,30h
int 21h
ret

printSpace:    mov ah,2
mov dl,32
int 21h
ret

code ends
end start```

Operation results: Source code:

```assume cs:code,ds:data
data segment
str db 'try'
len equ \$ - str
data ends
code segment
start:
mov ax,data
mov ds,ax
mov ax,0b800h ;es Play memory
mov es,ax
mov cx,0

mov bh,0  ;bh Specify row
mov bl,2h  ;bl String color,Green characters on black background
call printStr

mov bh,24
mov bl,4h ;Scarlet letter on black background
call printStr

mov ax,4c00h
int 21h

printStr:    mov si,offset str  ;String start offset address
mov cx,len ;String length

mov ah,0
mov al,bh ;ax Save specified row in
mov dx,160 ;160 bytes per line
mul dx ;Calculates the starting offset address of the specified row
mov di,ax ;mul 8 Bit results will be placed in ax in

s:  mov al,[si] ;Put it low first and save characters in low order
mov es:[di],al
mov es:[di+1],bl ;High level amplification attribute
inc si ;Move to next character
loop s
ret

code ends
end start```

Operation results: Source code:

```assume cs:code,ds:data
data segment
stu_no db '201983290331'
len = \$ - stu_no
data ends
code segment
start:    mov ax,data
mov ds,ax
mov ax,0b800h ;es Video memory address
mov es,ax

mov bx,1700h
call printCol

;Line 25 starts with an offset address of 3840
;24*80=1920 word =3840 byte
mov si,3840
mov cx,160 ;
call printLine

;len=12
mov si,3914 ;3840+(160-12)/2
mov cx,12
call printNum

mov ax,4c00h
int 21h

printCol:    mov si,0
mov cx,4000 ;25*80*2 byte
s:
mov es:[si],bl
mov es:[si+1],bh
loop s
ret

printLine:
mov bh,17h
mov bl,2dh
s1:
mov es:[si],bl
mov es:[si+1],bh
loop s1
ret

printNum:
mov di,0
s2:
mov bl,[di]
mov es:[si],bl
mov es:[si+1],bh
inc di
loop s2
ret

code ends
end start```

Operation results: Posted by Vanness on Mon, 29 Nov 2021 10:33:19 -0800