# [assembly experiment] Experiment 3 transfer instruction jump principle and its simple application programming

The program task1.asm source code is given

```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```

Answer question ① 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.

The IP jumps from 001B to 000D with an offset of - 14

Answer question ②   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.

The IP jumps from 0039 to 0029 with an offset of - 16

The program task2.asm source code is given

```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:[0], offset s1
mov word ptr ds:[2], offset s2
mov ds:[4], cs

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

call word ptr ds:[0]
s1: pop ax

call dword ptr ds:[2]
s2: pop bx
pop cx

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

① According to the jump principle of call instruction, it is analyzed theoretically that before the program executes to exit (line31), register (ax) =? Register (bx) =? Register (cx) =?

From line 16 to 18 of the program, we can see that the offset address of s1 is stored in ds:[0], the offset address of s2 is stored in ds:[1], and the segment address of data segment is stored in ds:[4]. In line 24, because the offset address is pushed into the stack by executing the call instruction, ds:[0] The word data stored in is the offset address of s1, so the offset address of s1 is stored in ax. In line 27, the double word data of ds:[2], that is, ds:[2] and ds:[4], so the offset address of s2 is stored in bx and the segment address of data segment is stored in cx.

That is, ax = ip of S1, BX = ip of S2, CX = cs of S2

② Assemble and link the source program to get the executable program task2.exe. Use debug to observe and verify whether the debugging results are consistent with the theoretical analysis results.

It can be found that ax = 4c21, bx = 0026, cx = 076c

This part of the experiment is mainly to separate the two digits into ten bits and one bit, then convert the two parts into ASCII code, and then use the No. 2 subroutine of int21h to output it

The program source code task3.asm is given

``` 1 assume cs:code, ds:data
2
3 data segment
4 x db 99, 72, 85, 63, 89, 97, 55
5 len equ \$- x
6 data ends
7
8 code segment
9 start:
10   mov ax, data
11   mov ds, ax;
12   mov si, 0
13   mov cx, len ;Data is byte，len Is the data length
14   s:
15    mov ah, 0 ;The number has only one byte, so ah=0
16    mov al, [si]
17    mov bx, offset printNumber
18    call bx
19    mov bx, offset printSpace
20    call bx
21    inc si
22    loop s
23   mov ah, 4ch
24   int 21h
25
26   printNumber:
27     mov bl, 10
28     div bl;Separate ten bits and one bit
29     mov bx, ax
30
31     mov dl, bl ;Quotient
32     add dl, 30h ;convert to ascii
33     mov ah, 2 ;call int 21h Subroutine 2 of
34     int 21h;
35
36     mov dl, bh ;Remainder
37     add dl, 30h ;convert to ascii
38     mov ah, 2 ;call int 21h Subroutine 2 of
39     int 21h;
40   ret
41
42   printSpace:
43     mov dl, ' '
44     mov ah, 2
45     int 21h
46   ret
47
48 code ends
49 end start```

Screenshot of running test

The program source code task4.asm is given

``` 1 assume ds:data, cs:code
2 data segment
3   str db 'try'
4   len equ \$ - str
5 data ends
6
7 code segment
8 start:
9   mov ax, data
10   mov ds, ax
11   mov ax, 0b800h
12   mov es, ax
13   mov si, 0
14   mov dh, 0 ;Line number
15   mov bl, 2 ;colour
16   call printStr
17
18   mov si, 0
19   mov dh, 24 ;Line number
20   mov bl, 4 ;colour
21   call printStr
22   mov ax, 4c00h
23   int 21h
24
25   printStr:
26     mov al, 160 ;One line can store 80 characters and 160 bytes
27     mul dh
28     mov dx, ax ;Relative addressing can only be used bx, bp, si, di
29     mov al, bl
30     mov bx, dx
31     mov cx, len
32     s:
33       mov dl, [si]
34       mov es:[bx], dl
35       inc bx
36       mov es:[bx], al
37       inc bx
38       inc si
39       loop s
40   ret
41 code ends
42 end start```

Screenshot of running test

The program source code task5.asm is given

``` 1 assume cs:code, ds:data
2 data segment
3   stu_no db '201983440007'
4   len = \$ - stu_no
5 data ends
6
7 code segment
8 start:
9   mov ax, data
10   mov ds, ax
11   mov ax, 0b800h
12   mov es, ax
13   ;Tone background color
14   mov si, 1
15   mov dl, 17h ; 0 001 0 111
16   mov cx, 2000
17   blue:
18     mov es:[si], dl
20     loop blue
21   ;Print horizontal lines
22   mov dh, 24
23   mov al ,160
24   mul dh ;Calculation start position
25   mov bx, ax
26   call line
27
28   mov cx, len
29   mov si, 0
30   sn:
31     mov dl, ds:[si]
32     mov es:[bx], dl
33     inc si
35     loop sn
36
37   call line
38   mov ax, 4c00h
39   int 21h
40
41   line:
42     mov dl, '-'
43     mov cx, 34 ;( 80- 12 ) / 2
44     s:
45       mov es:[bx], dl
47       loop s
48     ret
49 code ends
50 end start```

Screenshot of running test

Posted by stan801003 on Fri, 26 Nov 2021 04:33:08 -0800