Analysis of Checkpoint 16.2 Answers in Assembly Language

“Assembly Language” 3rd Edition by Wang Shuang

Chapter 16 Direct Addressing Table

Checkpoint 16.2 (Page 291)

The following program sums the 8 data points at location a in the data segment, storing the result in the word at location b. Complete the program.

assume cs:code, es:data

data segment

a db 1,2,3,4,5,6,7,8

b dw 0

data ends

code segment

start:

mov si, 0

mov cx, 8

s: mov al, a[si]

mov ah, 0

add b, ax

inc si

loop s

mov ax, 4c00H

int 21H

code ends

end start

=============Reference AnswerAnalysis Explanation:

The pseudo-instruction assume es:data tells the compiler: when accessing labels in the data segment, use the es register as the segment address register by default. When the code encounters labels a or b, the compiler automatically compiles them as es:[offset] (the segment address is provided by es, and the offset is calculated from the label’s position). For example, mov al, a will be compiled as mov al, es:[0] (the machine code includes offset 0, with the segment register implicitly being es), and the instruction mov ax, b will be compiled as mov ax, es:[8] (offset 8, with the segment register implicitly being es). The actual compiled result is shown in the figure below.

Analysis of Checkpoint 16.2 Answers in Assembly Language

This means that the compiler only decides how to generate the corresponding machine code based on the settings of assume (different registers generate different machine codes), but it does not automatically load the segment address of the data segment into es. Therefore, we need to explicitly load the actual segment address of the data segment into es in the code, so that the CPU can read the correct segment address from es when processing code related to labels in the data segment.

In summary: the assembler is responsible for “which register to use”, the programmer is responsible for “putting the correct value into the register”, and the CPU is responsible for “calculating the actual address using the value of the register”.

The reference answer is as follows:

mov ax, data

mov es, ax

Leave a Comment