ASSIGN OPERATOR: INTEL BRICKS

Back to:
index E97
I called bricks the short pieces of code, that "COMPAS" generate as a part of operator. Compiler builds result machine code from the bricks.

Step 1 (extract first operand to AX)

Step 2 (do operation or calculate function)

Step 3 (save result from AX)

Examples of full operator


Assign operator: step 1 (extract first operand to AX)

Extract simple variable (e.g. a) or variable with concrete index (e.g. b[2]) to AX (assume, that variable address is 490):
INTEGER type
A1 90 04 mov ax,[490]
CHAR or BOOLEAN
A0 90 04 mov al,[490]

Extract index variable (e.g. b[i]) to AX (assume, that i address is 490 and b[0] address is 4D0):
INTEGER type,
INTEGER index
8B 1E 90 04 mov bx,[490]

D1 E3 shl bx,1 (*2)
8B 87 D0 04
mov ax,[bx+4D0]

INTEGER type,
CHAR index
8A 1E 90 04 mov bl,[490]
30 FF xor bh,bh (0 ==> bh)
D1 E3 shl bx,1 (*2)
8B 87 D0 04
mov ax,[bx+4D0]

CHAR type,
INTERGER index
8B 1E 90 04 mov bx,[490]


8A 87 D0 04
mov al,[bx+490]
30 E4 xor ah,ah (0 ==>ah)
Red color is used to show the effect of index type, blue - array elements type.

Extract constant to AX (assume, that constant is 40):
for all types
B8 40 00 mov ax,40
In Intel code all constants are extracted equally.



Back to:
index top E97

Assign operator: step 2 (do operation or calculate function)

Add simple variable (e.g. a) or variable with concrete index (e.g. b[2]) to R1 (assume, that variable address is 490):
8B 0E 90 04 mov cx,[490]
01 C8 add ax,cx

Add index variable (e.g. b[i]) to AX (assume, that i address is 490 and b[0] address is 4D0):
8B 1E 90 40 mov bx,[490]
D1 E3 shl bx,1 (*2)
8B 8F D0 04 mov cx,[x+4D0]
01 C8 add ax,cx

Add constant to AX (assume, that constant is 40):
B9 40 00 mov cx,40
01 C8 add ax,cx

Different operations with simple variable (assume, that variable address is 490):
INTEGER operationsBOOLEAN operations
8B 0E 90 04 mov cx,[490]
01 C8 add ax,cx
8B 0E 90 04 mov cx,[490]
21 C8 and ax,cx
25 01 00 and ax,1 (low bit extract)
8B 0E 90 04 mov cx,[490]
29 C8 sub ax,cx
8B 0E 90 04 mov cx,[490]
09 C8 or ax,cx
25 01 00 and ax,1 (low bit extract)
8B 0E 90 04 mov cx,[490]
31 D2 xor dx,dx (clear DX)
F7 E1 mul ax,cx
8B 0E 90 04 mov cx,[490]
31 C8 xor ax,cx
25 01 00 and ax,1 (low bit extract)
8B 0E 90 04 mov cx,[490]
31 D2 xor dx,dx (clear DX)
F7 F1 div ax,cx
8B 1E 90 04 mov bx,[490]
E8 ?? ?? call 143 - MOD
89 D8 mov ax,bx (result to AX)
?? ?? in the last cell mean, that the contents of these bytes depend on instruction location - it's relative jump.

Calculation of functions:
INTEGER typeCHAR typeBOOLEAN type
31 D2 xor dx,dx (clear DX)
F7 E0 SQR( ): mul ax,ax
40 SUCC( ): inc ax F7 D0 NOT ax
25 01 00 and ax,1
(low bit extract)
E8 ?? ?? ABS( ): call 150 - ABS
48 PRED( ): dec ax

In conclusion I want to say honestly, that algorithm of this step translation to Intel codes is not optimal. I just realized the most universal and simple one.



Back to:
index top E97

Assign operator: step 3 (save result from AX)

Save simple variable (e.g. a) or variable with concrete index (e.g. b[2]) to AX (assume, that variable address is 490):
INTEGER type
A3 90 04 mov [490],ax
CHAR or BOOLEAN
A2 90 04 mov [490],al

Save index variable (e.g. b[i]) to AX (assume, that i address is 490 and b[0] address is 4D0; i and b have INTEGER type):
Step 1
8B 1E 90 04 mov bx,[490]
D1 E3 shl bx,1 (*2)
B8 D0 04 mov ax,4D0
01 D8 add ax,bx
89 C6 mov si,ax
Here we prepare b[i] address in SI
Step 2
...
Step 3
89 04 mov [si],ax
And now save AX, using SI



Back to:
index top E97

Examples of full operators

1. i := i + k (4FE - address of variable i, 4FC - k)
A1 FE 04 mov ax,[4FE] Extract i
8B 0E FC 04 mov cx,[4FC]
Extract k
01 C8 add ax,cx i + k
A3 FE 04 mov [4FE],ax Save i

2. k := SQR(i)
A1 FE 04 mov ax,[4FE] Extract i
31 D2 F7 E0
xor dx,dx
mul ax,ax
SQR(i)
A3 FC 04 mov [4FC],ax Save k

3. a[i] := b[i] + a[k]
8B 1E FE 04
D1 E3
B8 E6 04
01 D8
89 C6

mov bx,[4FE]
shl bx,1 (*2)
mov ax,4E6
add ax,bx
mov si,ax
Step 1:
Prepare a[i] address in SI
8B 87 D2 04 mov ax,[bx+4D2]
Extract b[i] to AX
(using BX above value)
8B 1E FC 04
D1 E3
8B 8F E6 04

mov bx,[4FC]
shl bx,1 (*2)
mov cx,[bx+4E6]
Step 2:
Extract a[k] to CX
01 C8 add ax,cx b[i] + a[k]
89 04 mov [si],ax
Step 3: result ==> a[i]



Back to:
index top E97