I have started to learn assembly. I came across these lines.
;*************************************************;
; Second Stage Loader Entry Point
;************************************************;
main:
cli ; clear interrupts
push cs ; Insure DS=CS
pop ds
Here on second line of code, the code segment is push to the stack(I think this). I have seen it in many codes. Why we should do this and how it ensures DS =CS? On third line DS is pop out of stack(I think this). Why it is done? It is pop out of stack means it was push to stack before. There is no code for that. Can anybody explain all this to me? Thanks in advance.
It’s not the
push csthat ensures this, it’s thepush cs; pop ds;combination that does.The first instruction copies the current value of
csonto the stack, and the second pulls that value off the stack and puts it into thedsregister.In response to your request for more information, let’s start with the following stack and registers:
After
push cs, which pushes the value of thecsregister onto the stack:After
pop ds, which pops a value off the stack and put it into thedsregister:And that’s basically it.
I can’t recall of the top of my head whether it was possible to transfer between segment registers with a
movinstruction (I don’t think it was, but I may be wrong, and this would necessitate the push/pop sequence). This link would seem to confirm that: there is nomovoption with a segment register as both source and destination.But even if it were, assembler coders often chose more suitable instructions, either for speed or compact code (or both), things like using
xor ax, axinstead ofmov ax, 0for example.