汇编利用编译器简化代码
作者:陆麟
转载请征得作者同意.
2001.3.2
我们在汇编里可以用编译器指示来自动生成代码,简化代码写作.下面是循环的例子.
.386p
.model tiny
code segment use16
org 100h
start:
mov ecx,20000h
;;用下面的指示告诉编译器自动生成循环代码.
;;不一定是ECX,其他寄存器一样可以用.
.while ecx>0h
dec ecx
cmp ecx,10000h
jnz continue
mov edx,offset msg
mov ah,9
int 21h
continue:
;;与.while指示匹配
.endw
mov ah,4ch
int 21h
msg byte "ECX reachs 10000h",0dh,0ah,"$"
code ends
end start
生成的代码十分简洁.下面是编译器自动生成的代码(注意,这是SOURCER V7.0逆向生成的源代码,比源代码的注解还多.):
.586p
.387
seg_a segment byte public use16
assume cs:seg_a, ds:seg_a
org 100h
whiletst proc far
start::
mov ecx,20000h
jmp short loc_2
loc_1::
dec ecx
cmp ecx,10000h
jne short loc_2 ; Jump if not equal
mov edx,offset32 data_1 ; ('ECX reachs 10000h')
mov ah,9
int 21h ; DOS Services ah=function 09h
; display char string at ds:dx
loc_2::
cmp ecx,0
ja loc_1 ; Jump if above
mov ah,4Ch
int 21h ; DOS Services ah=function 4Ch
; terminate with al=return code
data_1 db 'ECX reachs 10000h', 0Dh, 0Ah, '$'
whiletst endp
seg_a ends
end start
通过该指示的使用,我们可以使用接近C语法的汇编段落了.对于习惯C的程序员来说不错.