Wang Shuang compiles the tenth chapter assignment post

Source: Internet
Author: User
Tags mul

Chapter 10 call and RET commands

10.1 Detection Points
Complete the program to execute commands starting from in memory
Assume Cs: Code
Stack segment
DB 16 DUP (0)
Stack ends

Code segment
Start: mov ax, stack
MoV SS, ax
MoV sp, 16
MoV ax, 1000 h
PUSH AX
MoV ax, 0
PUSH AX
Retf
Code ends
End start

Detection site 10.2
What is the value in ax after the following program is executed?

Memory Address machine code assembly command
1000:0 B8 00 00 mov ax, 0
1000:3 E8 01 00 call S
1000:6 40 Inc ax
1000:7 58 s: Pop ax

Ax = 6; after debugging, it is found that the IP address is 7, and the IP address is 6 after pop.

 

Detection site 10.3
What is the value in ax after the following program is executed?
Memory Address machine code assembly command
1000:0 B8 00 00 mov ax, 0
1000:3 9A 09 00 00 10 call far PTR s
1000:6 40 Inc ax
1000:7 58 s: Pop ax
Add ax, ax
Pop BX
Add ax, BX

Ax = 8; debug result

 

Detection site 10. 4

What is the value in ax after the following program is executed?
Memory Address machine code assembly command
1000:0 B8 06 00 mov ax, 6
1000:2 FF D0 call ax Inc ax
1000:5 40 Inc ax
1000:6 mov bp, SP
Add ax, [BP]

Ax = 000b; the test result starts with the next command.

Detection site 10. 5
1. What is the value in ax after the following program is executed? (Note: the principle of the Call command is used for analysis. No
It is difficult to prove your conclusion by tracking in a single step in debug. For this program, the result of one-step tracking in debug,
Cannot represent the actual execution result of the CPU .)
Assume Cs: Code
Stack segment
DW 8 DUP (0)
Stack ends
Code segment
Start: mov ax, stack
MoV SS, ax
MoV sp, 16
MoV ds, ax
MoV ax, 0
Call word ptr ds: [0eh]
INC ax
INC ax
INC ax
MoV ax, 4c00h
Int 21 h
Code ends
End start

This question has questions about debug tracking !!!
Direct Analysis AX = 0003

Use DEBUG to trace the result as follows:
Ax = 0000; the DS: [0eh] of the call was redirected, and INC ax was not executed down.

2. After the following program is executed, what are the values in ax and BX?
Assume Cs: Code
Data Segment
DW 8 DUP (0)
Data ends
Code segment
Start: mov ax, Data
MoV SS, ax
MoV sp, 16
MoV word ptr ss: [0], offset s
MoV SS: [2], CS
Call dword ptr ss: [0]
NOP
S: mov ax, offset s
Sub ax, SS: [0ch]
MoV BX, CS
Sub BX, SS: [0eh]
MoV ax, 4c00h
Int 21 h
Code ends
End start

Ax = 0001, BX = 0000

 

Lab 10 program subprograms

1. Display strings.
Assume Cs: Code
Data Segment
DB 'Welcome to MASM! ', 0
Data ends

Code segment
Start: mov DH, 8
MoV DL, 3
MoV Cl, 2
MoV ax, Data
MoV ds, ax
MoV Si, 0
Call show_str

MoV ax, 4c00h
Int 21 h
Show_str:
Push Si
Push CX
Push BX
Push DX
PUSH AX

MoV ax, 0b800h
MoV es, ax

MoV Al 160
MoV BL, DH
Dec BL
Mul BL; AX = number of incoming rows
MoV BX, ax
MoV Al, 2
Mul dl; BX = row + Column
Add Bx, ax;
MoV Di, BX

Change: mov Cl, [Si]
MoV CH, 0
Jcxz OK
MoV Al, DS: [Si]
MoV ah, 2 h
MoV ES: [di], ax
 
Add Di, 2
INC Si
JMP short change
 
OK: Pop ax
Pop DX
Pop BX
Pop CX
Pop Si
RET

Code ends
End start

 

;
; A sample code of Division
Assume Cs: Code
Code segment

Start: mov dx, 1; 32-bit Division
MoV ax, 86a1h
MoV X, 100
Div BX

MoV ax, 1001; 16-bit Division
MoV BL, 100
Div BL
 
MoV ax, 4c00h
Int 21 h

Code ends
End start

 

 

 

 

2. Solve the Problem of Division Overflow

Assume Cs: code, DS: data, SS: Stack

Data Segment
DB 16 DUP (0)
Data ends

Stack segment
DB 16 DUP (0)
Stack ends

Code segment
Start:
MoV ax, stack
MoV SS, ax
MoV sp, 16
MoV ax, Data
MoV ds, ax
 
MoV ax, 4240 H
MoV dx, 000fh
MoV CX, 0ah
Call divdw

MoV ax, 4c00h
Int 21 h

Divdw:
MoV BX, ax; cache ax-16 lower Divisor
MoV ax, DX; AX = H, 16-bit high Divisor
MoV dx, 0
Div CX; ax is quotient, dx is remainder = REM (H/n) * 65536
Push ax; Result provider, that is, put in DX
MoV ax, BX; dx is REM (H/n) * 65536, high 16 bits, ax is low 16 bits, and a division operation is performed again
Div CX; ax is the quotient, the last result is 16 bits, dx is the remainder -- is the final result and should be assigned to CX
MoV CX, DX
Pop DX

RET

Code ends
End start

3. Numerical Display
For more information about this code, see the online code.
Program to display the data in the data segment in decimal format
Assume Cs: Code

Data Segment
DB 10 DUP (0)
Data ends

Stack segment
DB 16 DUP (0)
Stack ends

Code segment
Start:
MoV ax, Data
MoV ds, ax

MoV ax, stack
MoV SS, ax
MoV sp, 16

MoV dx, 0c1h
MoV ax, 76f3h
MoV Si, 0

Call dwtoc

MoV DH, 8
MoV DL, 3
MoV Cl, 2
 
Call show_str

MoV ax, 4c00h
Int 21 h

; Subroutine: dwtoc
Function: converts DWORD data into a decimal string that ends with 0.
Parameter: (ax) = low 16-bit DWORD data
; (Dx) = high 16-bit DWORD data
; DS: Si points to the first address of the string
; Return: None
Dwtoc:
MoV CX, 0
Push CX

S_dwtoc:
MoV CX, 10; Divisor
Call divdw; the remainder is in CX

Add CX, 30 h
Push CX; Save the remainder in ASCII format

Determine whether the quotient is 0. If both the 16-bit and high-bit values are 0, return
MoV CX, DX
Jcxz OK _dxz;

If the value is not 0, the system directly jumps back and continues the operation.
JMP short s_dwtoc

The operator's high level is 0.
OK _dxz:
MoV CX, ax
Jcxz OK _axz
JMP short s_dwtoc

The low position of the provider is 0.
OK _axz:
; Assigned to DS: [Si]
MoV dx, Si; save Si. Si is the first address of the string.
Loop_dtoc:
Pop CX
MoV DS: [Si], Cl
Jcxz end_dwtoc
INC Si
JMP short loop_dtoc

MoV Si, DX
 
End_dwtoc:
MoV Si, DX
RET

DTOC:
First put a 0 into the stack. When S2 is removed from the stack, you can jump to the stack based on CX 0.
MoV CX, 0
Push CX
S1_dtoc:
MoV dx, 0
MoV CX, 10
Div CX

MoV CX, DX; DX Remainder
Add CX, 30 h
Push CX; save in stack

MoV CX, ax; ax is the operator. When the operator is 0, the values of all of you will have been obtained, and you can jump out of the loop.
Jcxz ok1_dtoc

JMP short s1_dtoc
 
Ok1_dtoc:
MoV CH, 0
S2_dtoc:
; Remove from the stack
Pop CX
Jcxz ok2_dtoc
MoV DS: [Si], Cl
INC Si
JMP short s2_dtoc

Ok2_dtoc:
RET
 
Show_str:
PUSH AX
Push BX
Push CX
Push DX
Push es
Push Si

The address y = 160 * (number of rows-1) + number of columns * 2-2, b800: Y
; Cyclically write the strings in the parameters into the memory of the video card, and return the results if 0 is detected.

; BX = 160 * (number of rows-1)
MoV BH, DH
Sub BH, 1
MoV Al 160
Mul BH
MoV Si, ax; Si is the offset value calculated based on the number of rows

; AX = number of columns * 2-2
MoV BL, DL
MoV Al, 2
Mul BL
Sub ax, 2; offset value calculated based on the number of Columns
Add Si, ax; The sum of the number of rows and columns is in Si

MoV ax, 0b800h
MoV es, ax

MoV BX, Si; Save the SI value in BX, and BX shows the offset value for the display.
MoV Si, 0; question requirements...

MoV DL, Cl; Save the font color attribute
MoV CH, 0
S:
MoV Cl, DS: [Si]
MoV ES: [BX], Cl
Jcxz OK

MoV ES: [bx + 1], DL

INC Si
Add Bx, 2
JMP short S
OK:
Pop Si
Pop es
Pop DX
Pop CX
Pop BX
Pop ax

RET

; Subroutine: divdw
; Requirement: Perform Division operations without division overflow. The divisor is DWORD, the divisor is word, and the result is DWORD.
; Parameter: (ax) = the lowest 16 bits of the divisor DWORD type
; (Dx) = high 16-bit divisor DWORD type
; (CX) = Divisor
; Return: (dx) = 16-bit high of the result
; (Ax) = low 16 bits in the result
; (CX) = Remainder
Divdw:
MoV BX, ax; cache ax-16 lower Divisor
MoV ax, DX; AX = H, 16-bit high Divisor
MoV dx, 0
Div CX; ax is quotient, dx is remainder = REM (H/n) * 65536
 
Push ax; Result provider, that is, put in DX

MoV ax, BX; dx is REM (H/n) * 65536, high 16 bits, ax is low 16 bits, and a division operation is performed again
Div CX; ax is the quotient, the last result is 16 bits, dx is the remainder -- is the final result and should be assigned to CX

MoV CX, DX
Pop DX

RET

Code ends
End start

 

Lesson 1: refer to the Code on the Internet.
; Struct Definition
; Char year [4] // year
; Space (1 byte)
; Int income (4 bytes) // income
; Space
; Empoyer num (2 bytes) // Number of employees
; Space
; Per capita income (2 bytes)
; Space

; Requirement: copy the data in the data segment to the table segment, and construct the data in the preceding format. Then, calculate the per capita income for 21 years.

Assume DS: data, ES: Table, CS: code, SS: Stack

Data Segment
DB '000000', '000000', '000000', '000000', '000000', '000000', '000000', '000000'
DB '000000', '000000', '000000', '000000', '000000', '000000', '000000', '000000'
DB '000000', '000000', '000000'

DD 16, 22,382,135 6, 2390,800 0, 16000,244 86, 50065,974 79, 140417,197 514
Dd 345980,590 827, 803530,118, 1843000,275, 3753000,464, 5937000

DW 3, 7, 9, 13, 28, 38,130,220,476,778,100 1, 1442,225 8, 2793,403 7, 5635,822 6
DW 11542,144 30, 15257,178 00
Data ends

Table segment
DB 21 DUP ('year summ ne ?? ')
Table ends

Display segment
DB 21 DUP ('year summ ne ?? ')
Display ends

It's useless to get a stack.
.
Stack segment
DW 16 DUP (0)
Stack ends

Code segment

Start:

Set the data segment, and the first unit that DS: BX points to the data segment. That is, the content of DS: [BX] is the content of the first unit of the Data Segment.
MoV ax, Data
MoV ds, ax

; Set the table segment
MoV ax, table
MoV es, ax

; Set the stack segment
MoV ax, stack
MoV SS, ax
MoV sp, 16

; Initialize three address register
MoV BX, 0
MoV Si, 0
MoV Di, 0

; Prepare for replication. Use a loop for 21 times.
MoV CX, 21

S_start:
Year
MoV ax, DS: [bx + 0]; here 0 is written for the following comparison, clear
MoV ES: [Si + 0], ax
MoV ax, DS: [bx + 2]
MoV ES: [Si + 2], ax

; Space
MoV Al, 32
MoV ES: [Si + 4], Al

; Income
MoV ax, DS: [bx + 84]
MoV ES: [Si + 5], ax
MoV ax, DS: [bx + 86]
MoV ES: [Si + 7], ax

; Space
MoV Al, 32
MoV ES: [Si + 9], Al

Number of employees, handled with caution
MoV ax, DS: [di + 168]
MoV ES: [Si + 0ah], ax

; Space
MoV Al, 32
MoV ES: [Si + 0ch], Al

Calculate the per capita income.
MoV ax, DS: [bx + 84]
MoV dx, DS: [bx + 86]
Push CX; temporarily use CX, because Div DS: [bx + 168] is not allowed.
MoV CX, DS: [di + 168]
Div CX
Pop CX
MoV ES: [Si + 0dh], ax

; Space
MoV Al, 32
MoV ES: [Si + 0fh], Al

Add Si, 16
Add Bx, 4
Add Di, 2; Remember to add 2
Loop s_start

The data has been arranged in the table segment, and the table segment starts at ES: [0 ].
The following is how to read the data, calculate the rows and columns, and display them on the screen.
Structured Data is arranged in bytes as follows:
0123 (character) 4 spaces (character) 5678 income (data) 9 space a B Number of employees (data) c space d e per capita income (data) F SPACE
; Total 21 years
The income, number of employees, and per capita income must be converted into character formats.

MoV ax, display
MoV ds, ax

MoV Si, 0
MoV Di, 0
MoV BX, 0
MoV CX, 21

Loop_display:
Push CX

##### Year
MoV Si, 0
MoV ax, ES: [di]
MoV DS: [Si], ax
MoV ax, ES: [di + 2]
MoV DS: [Si + 2], ax
MoV ax, 0
MoV DS: [Si + 4], ax; the original Display Error. mov DS: [Si + 5], ax, the error cannot be a single byte...
; Display year
MoV DL, 20; 20th Columns
Call dis

##### Income
MoV ax, ES: [di + 5]; 16-bit low
MoV dx, ES: [di + 7]; 16-bit high
MoV Si, 0
Call dwtoc; DS: Si points to the first address of the string
; Show revenue
MoV DL, 28; 28th Columns
Call dis

##### Number of employees
MoV ax, ES: [di + 0ah]
MoV Si, 0
Call DTOC
; Display the number of employees
MoV DL, 36; column 36th
Call dis

##### Per capita income
MoV ax, ES: [di + 0dh]
MoV Si, 0
Call DTOC
; Show per capita income
MoV DL, 44; 44th Columns
Call dis

Add Di, 16
Pop CX
Sub CX, 1
Jcxz end_main
JMP near PTR loop_display

End_main:
MoV ah, 01 H
Int 21 h

MoV ax, 4c00h
Int 21 h

; Subroutine dis
Function: encapsulate some identical operations
; Parameter: (DL) indicates the number of columns.
DIS:
PUSH AX
Push BX
Push CX
Push DX
Push di
Push Si
MoV ax, Di
MoV DH, 16
Div DH
MoV DH, Al
Add DH, 2; DH = di/16 + 2
MoV Si, 0
MoV Cl, 2
Call show_str

Pop Si
Pop di
Pop DX
Pop CX
Pop BX
Pop ax
RET

; Subroutine: DTOC
Function: converts word data into a decimal string that ends with 0.
; Parameter: (ax) = word data
; DS: Si points to the first address of the string
; Return: None
DTOC:
First put a 0 into the stack. When S2 is removed from the stack, you can jump to the stack based on CX 0.
MoV CX, 0
Push CX
S1_dtoc:
MoV dx, 0
MoV CX, 10
Div CX

MoV CX, DX; DX Remainder
Add CX, 30 h
Push CX; save in stack

MoV CX, ax; ax is the operator. When the operator is 0, the values of all of you will have been obtained, and you can jump out of the loop.
Jcxz ok1_dtoc

JMP short s1_dtoc
 
Ok1_dtoc:
MoV CH, 0
S2_dtoc:
; Remove from the stack
Pop CX
MoV DS: [Si], Cl
Jcxz ok2_dtoc
INC Si
JMP short s2_dtoc

Ok2_dtoc:
RET

; Name: show_str
Function: Specify the position and color to display a string ending with 0
; Parameter: (DH) = row number (0--24), (DL) = column number (0--79)
; (CL) = color, DS: Si points to the first address of the string
; Return: None
Show_str:
PUSH AX
Push BX
Push CX
Push DX
Push es

The address y = 160 * (number of rows-1) + number of columns * 2-2, b800: Y
; Cyclically write the strings in the parameters into the memory of the video card, and return the results if 0 is detected.

; BX = 160 * (number of rows-1)
Sub DH, 1 h
MoV Al 160
Mul DH
MoV BX, ax; BX is the offset value calculated based on the number of rows

; AX = number of columns * 2-2
MoV Al, 2
Mul DL
Sub ax, 2; offset value calculated based on the number of Columns
Add Bx, ax; The sum of the number of rows and columns exists in BX.

MoV ax, 0b800h
MoV es, ax

MoV DL, Cl; Save the font color attribute
MoV CH, 0

S_show_str:
MoV Cl, DS: [Si]
MoV ES: [BX], Cl
Jcxz OK _show_str

MoV ES: [bx + 1], DL

INC Si
Add Bx, 2
JMP short s_show_str

OK _show_str:
Pop es
Pop DX
Pop CX
Pop BX
Pop ax

RET

; Subroutine: dwtoc
Function: converts DWORD data into a decimal string that ends with 0.
Parameter: (ax) = low 16-bit DWORD data
; (Dx) = high 16-bit DWORD data
; DS: Si points to the first address of the string
; Return: None
Dwtoc:
MoV CX, 0
Push CX

S_dwtoc:
MoV CX, 10; Divisor
Call divdw; the remainder is in CX

Add CX, 30 h
Push CX; Save the remainder in ASCII format

Determine whether the quotient is 0. If both the 16-bit and high-bit values are 0, return
MoV CX, DX
Jcxz OK _dxz;

If the value is not 0, the system directly jumps back and continues the operation.
JMP short s_dwtoc

The operator's high level is 0.
OK _dxz:
MoV CX, ax
Jcxz OK _axz
JMP short s_dwtoc

The low position of the provider is 0.
OK _axz:
; Assigned to DS: [Si]
MoV dx, Si; save Si. Si is the first address of the string.
Loop_dtoc:
Pop CX
MoV DS: [Si], Cl
Jcxz end_dwtoc
INC Si
JMP short loop_dtoc

MoV Si, DX
 
End_dwtoc:
MoV ax, 0
MoV DS: [Si], ax
MoV Si, DX
RET

; Subroutine: divdw
; Requirement: Perform Division operations without division overflow. The divisor is DWORD, the divisor is word, and the result is DWORD.
; Parameter: (ax) = the lowest 16 bits of the divisor DWORD type
; (Dx) = high 16-bit divisor DWORD type
; (CX) = Divisor
;
; Return: (dx) = 16-bit high of the result
; (Ax) = low 16 bits in the result
; (CX) = Remainder
Divdw:
MoV BX, ax; cache ax-16 lower Divisor
MoV ax, DX; AX = H, 16-bit high Divisor
MoV dx, 0
Div CX; ax is quotient, dx is remainder = REM (H/n) * 65536
 
Push ax; Result provider, that is, put in DX

MoV ax, BX; dx is REM (H/n) * 65536, high 16 bits, ax is low 16 bits, and a division operation is performed again
Div CX; ax is the quotient -- the lowest 16 bits in the final result, dx is the remainder -- is the final result, and should be assigned to CX

MoV CX, DX
Pop DX

RET

Code ends
End start

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.