Why assembler insert some strange instructions at my code?
I written some code for meet requirements registers preservation for some third party boot loader kernel system On Time RTOS-32. You can see requirements at url at code for MASM32 below. Need MZ-file. Code will run at x86 16bit real segmented mode at modern CPUs. I try use MASM32 and fasm. Both insert nearly same strange instructions at my code, that I not written there. Why is this happened?
My code:
;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
I use hiew disassembler (and debug.com and debugx.com) for see output of assembler.
Also I try trace at debugx.com. That strange instructions realy executed. It is not disassembler's decode error (if debugx not have emulator for trace).
disassembler result at 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
How I can fix it?