I would like to know if it is possible (and if so, how) to write a sequence of instructions that would have the same effect as push
. For example, if the contents of ax
is 1200 , and I do a push ax
, what other instructions can I use to accomplish what push ax
does?
相关问题
- Null-terminated string, opening file for reading
- What's the difference between 0 and dword 0?
- Ada beginner Stack program
- Translate the following machine language code (0x2
- Where can the code be more efficient for checking
相关文章
- Threading in C# , value types and reference types
- Is it possible to run 16 bit code in an operating
- Stack<> implementation in C#
- How to generate assembly code with gcc that can be
- Select unique/deduplication in SSE/AVX
- Optimising this C (AVR) code
- Java, Printing the stack values
- Why does the latency of the sqrtsd instruction cha
Some other answers use
[sp]
for stack addressing, but it is not possible in 16-bit mode, nor in 32-bit or 64-bit modes either. However, in 32-bit mode you can use[esp]
and in x86-64 you can use[rsp]
for memory addressing, but in 16-bit mode there is no memory addressing that usessp
. See here for possible memory addressing modes in 16-bit mode.So, what you need to do: store the value of
bp
somewhere, copysp
intobp
, then usebp
for addressing the stack, and finally restore the original value ofbp
.If you have a place where to store
bp
, that's easy (this is in YASM/NASM syntax):Using a register instead of memory address like
bp_storage
here is trivial too.Edit: Added version that does not modify flags (below), as
push
doesn't modify flags either.The code above modifies flags, whereas
push ax
does not modify any flags. That can be solved by storing firstah
into memory, then loading flags intoah
withlahf
, then storing the flags fromah
to memory, then modifying the stack as above, and then afterwards restoring flags from memory viaah
by usingsahf
and finally restoringah
from memory.Edit: To simulate
push ax
without changes in flags,ah
must be saved beforelahf
and loaded beforemov [bp],ax
. Fixed.sub
modifiesAF
,CF
,OF
,PF
,SF
,ZF
, whereaslahf
loads andsahf
stores onlyAF
,CF
,PF
,SF
,ZF
(noOF
). However,sp
should never overflow in normal stack usage.But, if you can't access memory, and want to use stack to store
bp
you can do that, but if you neither have free registers to use, things get complicated. But if you are using a real mode OS, you can block interrupts withcli
, exchangebp
andsp
, usebp
for stack addressing, exchangebp
andsp
again and allow interrupts again withsti
.Edit: the value of
sp
needs to subtracted by 2 to simulatepush ax
. Fixed. This version does not modify flags (except interrupt flag).At least if memory serves, it's roughly equivalent to:
Subtract a value that equals the size of the data write required from the sp, then move/write on the stack the objects you want. Compilers do this sort of thing all the time. Look at a -S output for examples. Beware of atomic/thread issues if you do this...
If I've not forgotten the Intel syntax:
I've used
lea
to avoid touchingFLAGS
(neitherpush
normov
orlea
touch them, butsub
anddec
do).EDIT: It turns out that I've forgotten more important thing: there is no
[sp]
addressing mode. The correct answer is the one by @nrz, mine could be applied toesp
andeax
(would belea esp,[esp-4]
) on 80386 and above.