为什么汇编程序在我的代码中插入一些奇怪的指令

时间:2013-12-13 10:17:03

标签: assembly intel

我为一些第三方引导加载程序内核系统On Time RTOS-32编写了一些满足需求寄存器保存的代码。您可以在下面的MASM32代码中看到URL的要求。需要MZ文件。代码将在现代CPU的x86 16位实分段模式下运行。我尝试使用MASM32和fasm。两者都在我的代码中插入几乎相同的奇怪指令,我没有在那里写。为什么会这样? 我的代码:

;See table with columns "Register   Contains    Preserve" at and of document:
; http://www.on-time.com/rtos-32-docs/rttarget-32/programming-manual/rtloc/initializing-target-hardware/initcode.htm
;assembler commandline:
;   ml /c f.asm
;   link16 f
.686
.model tiny
ASSUME SS:NOTHING, DS:NOTHING, CS:ERROR, ES:ERROR, GS:ERROR, FS:ERROR
.code; code segment
main:;entry point to program
jmp cseg; jump to code get over data
; global variables:
ss_     dd ?
esp_    dd ?
;stack:
stack_reserved db 60 dup (?)
stacktop dd ?
;save register for meet requirements RTOS and
;intialize registers for work:
cseg:
mov eax, cs
mov edx, ds;ds saved at edx. edx and eax not preserved, because needn't.
mov ds, eax
mov eax, ss
mov ss_, eax
mov eax, esp
mov esp_, eax
mov eax, ds
mov ss, eax
mov esp, DWORD PTR stacktop
pushfd
pushad
mov eax, es
push eax;
mov eax, fs
push eax;
mov eax, gs
push eax;
;do work:
;...
;do some work. omit it.
;...
;restore preserved registers:
pop eax
mov gs, eax
pop eax
mov fs, eax
pop eax
mov es, eax
popad
popfd
mov eax, esp_
mov esp, eax
mov eax, ss_
mov ss, eax
mov ds, edx
jmp ebx;return to near absolute address
end main

我使用hiew反汇编程序(以及debug.com和debugx.com)来查看汇编程序的输出。 我也尝试在debugx.com上跟踪。那些奇怪的指令真的被执行了。它不是反汇编程序的解码错误(如果debugx没有用于跟踪的仿真器)。 反汇编结果在hiew:

00000200: EB48                           jmps        00000024A
;it is ok. it data segment with zero initialized.
00000202: 0000                           add         [bx][si],al
00000204: 0000                           add         [bx][si],al
...
00000246: 0000                           add         [bx][si],al
00000248: 0000                           add         [bx][si],al
0000024A: 8CC8                           mov         ax,cs
0000024C: 8CDA                           mov         dx,ds
0000024E: 8ED8                           mov         ds,ax
00000250: 8CD0                           mov         ax,ss
00000252: 2EA30200                       mov         cs:[00002],ax
;what is happened? add ... [si]...? I not written it.
00000256: 0000                           add         [bx][si],al
00000258: 8BC4                           mov         ax,sp
0000025A: 2EA30600                       mov         cs:[00006],ax
;same
0000025E: 0000                           add         [bx][si],al
00000260: 8CD8                           mov         ax,ds
00000262: 8ED0                           mov         ss,ax
;same oh ... five lines...
00000264: 2E8B25                         mov         sp,cs:[di]
00000267: 46                             inc         si
00000268: 0000                           add         [bx][si],al
0000026A: 009C608C                       add         [si][-073A0],bl
0000026E: C0508CE0                       rcl         b,[bx][si][-074],0E0 ;'р'
;why all it is happened?
00000272: 50                             push        ax
00000273: 8CE8                           mov         ax,gs
00000275: 50                             push        ax
00000276: 58                             pop         ax
00000277: 8EE8                           mov         gs,ax
00000279: 58                             pop         ax
0000027A: 8EE0                           mov         fs,ax
0000027C: 58                             pop         ax
0000027D: 8EC0                           mov         es,ax
0000027F: 61                             popa
00000280: 9D                             popf
00000281: 2EA10600                       mov         ax,cs:[00006]
00000285: 0000                           add         [bx][si],al
00000287: 8BE0                           mov         sp,ax
00000289: 2EA10200                       mov         ax,cs:[00002]
0000028D: 0000                           add         [bx][si],al
0000028F: 8ED0                           mov         ss,ax
00000291: 8EDA                           mov         ds,dx
00000293: FFE3                           jmp         bx

我该如何解决?

1 个答案:

答案 0 :(得分:1)

正如迈克尔所说,你是在16位模式下拆解你的代码,但它显然是在32位模式下组装的。要么以16位模式组装(可能是正确的选项),要么使用32位反汇编(在Hiew中使用Ctrl-F1)。

相关问题