bright@Data-IO.COM (Walter Bright) (05/23/89)
In examining the Windows prolog and epilog code for functions, it is: prolog: mov AX,DS ;????? nop ;????? inc BP ;????? push BP ;save previous mov BP,SP ;setup frame pointer for locals push DS mov DS,AX sub SP,xx ;make room for locals epilog: sub BP,2 ;account for the PUSH DS mov SP,BP ;remove locals pop DS pop BP dec BP ;???? ret Can anybody explain what's going on with the DS manipulation, the NOP and the inc and dec of BP? I suspect that the MOV AX,DS NOP is replaced at runtime with a MOV AX,VALUE, but I can't figure the INC BP.
paulc@microsoft.UUCP (Paul Canniff 2/1011) (05/23/89)
In article <1992@dataio.Data-IO.COM> bright@dataio.Data-IO.COM (Walter Bright) writes: >In examining the Windows prolog and epilog code for functions, it is: >prolog: > mov AX,DS ;????? > nop ;????? > inc BP ;????? > push BP ;save previous > mov BP,SP ;setup frame pointer for locals > push DS > mov DS,AX > sub SP,xx ;make room for locals > >epilog: > sub BP,2 ;account for the PUSH DS > mov SP,BP ;remove locals > pop DS > pop BP > dec BP ;???? > ret > >Can anybody explain what's going on with the DS manipulation, the NOP >and the inc and dec of BP? I suspect that the MOV AX,DS NOP is replaced >at runtime with a MOV AX,VALUE, but I can't figure the INC BP. Your assumption on DS is correct. The BP stuff is to allow Windows to "walk the stack" backwards, differentiating far calls from near calls, so it can move data around. When it moves data, it needs to clean up all those "pushed" DS values that are on the stack, or else when you start returning from functions you will be loading DS with an out-of-date value. And when it moves code, it must change any RETF's (actually the value of 'CS' on the stack) to the new value. And if code is discarded, RETF's actually jump to the cde which reloads the application code segment. Windows could not know which functions were entered with a FAR call and which with a NEAR. If that were the case it would be impossible to "walk the stack" accurately. By tweaking BP on each FAR function entry, it is possible to tell NEAT from FAR. For more info on this and other such tidbits, I recommend picking up _Programming Windows_ (CHarles Petzold, Microsoft Press), which is a great addition to any Windows SDK.
bturner@hpcvlx.HP.COM (Bill Turner) (05/23/89)
> In examining the Windows prolog and epilog code for functions, it is: > prolog: > [deleted] > Can anybody explain what's going on with the DS manipulation, the NOP > and the inc and dec of BP? I suspect that the MOV AX,DS NOP is replaced > at runtime with a MOV AX,VALUE, but I can't figure the INC BP. I strongly suggest you read Petzold's "Programming Windows", Chapter 8 (Memory Management), especially the section on "How Windows Moves and Reloads Program Segments." First, I'll say "you don't want to know this..." But if you do want to know, Petzold is the only place I've seen a good, concise description of what's going on. (I was told by one of the Windows developers) This is basically stuff to handle the fact that in Windows the code segments may be moving (if the old BP value on the stack is odd, then this is a FAR call frame). The DS junk is dealing with the fact that data segments may also move, and that a single program may have multiple instances (multiple data segments). Read Petzold. --Bill Turner (bturner@hp-pcd.hp.com) HP Corvallis Information Systems