汇编语言中,push操作为什么是给指针做减法? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
breeswish
V2EX    问与答

汇编语言中,push操作为什么是给指针做减法?

  •  
  •   < href="/member/breeswish">breeswish 2013-07-07 02:51:13 +08:00 4578 次点击
    这是一个创建于 4481 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如题。

    我个人的思考是,这样做

    - 不需要额外存大小了(比较指针是否到0来判断是否满)

    - 也许可以保护数据?(因为有段寄存器)

    不知道大家是如何考虑的呢
    9 条回复    1970-01-01 08:00:00 +08:00
    013231
        1
    013231  
       2013-07-07 02:56:14 +08:00   1
    向低位增, 入後地址得更小, 自然是法.
    detailyang
        2
    detailyang  
       2013-07-07 08:59:34 +08:00   2
    蛋疼...栈往向下长 堆往上长 = =
    cat /proc/pid/maps
    timonwong
        3
    timonwong  
       2013-07-07 10:43:28 +08:00   2
    体系结构不同而已,也有往上长的,说个还算比较常见的: 8051
    breeswish
        4
    breeswish  
    OP
       2013-07-07 11:42:10 +08:00
    @013231
    @detailyang 这样设计的理由是啥呢~
    detailyang
        5
    detailyang  
       2013-07-07 11:42:58 +08:00
    @breeswish 楼主...我也不懂为什么这样设计栈
    timonwong
        6
    timonwong  
       2013-07-07 11:55:20 +08:00   1
    @breeswish
    有一定历史原因,对于统一编址的结构(现在考虑古老的8086,最大8KiB的线性寻址),PC如果从0开始,栈放哪儿呢,2K,4K,然后保留给程序代码的地址空间要多少?如果代码只用了128字节,栈底从4K往上长,那就有一大堆内存给浪费了。
    lldong
        7
    lldong  
       2013-07-07 12:07:40 +08:00   1
    This enormous 64 bits worth of address space is divided up into two areas: The stack and the heap. The stack is an area set aside high in the address space (typically high, anyway; in practice it can be just about anywhere) for the use of subroutine calls and local variable storage. The stack always grows downward; as the amount of information on the stack increases, the address of the top of the stack decreases. On older systems with smaller memory models, it was possible for the stack to grow too far downward and collide with other areas, but while it's still technically possible for this to happen, other things would go wrong long before a heap collision (in particular, the stack would run off the edge of its allocated memory pages and cause a protection fault). The CPU has a few instructions specifically designed for manipulating the stack, though they often go unused in favor of more efficient methods in modern code. You can think of the stack as a moderately large chunk of memory allocated by the system at the launch of your program.

    The heap effectively consists of every area of memory that is not the stack; memory from the heap is allocated at runtime by the system for the process' use. The heap contains the stack, in fact, though they are usually considered conceptually separate. All of your executable code is loaded into a section of the heap, as well as copies of any libraries your executable links to. Note: These are not actually copies, as it would be ridiculously inefficient to copy every library for every loaded process, but it's easier to just think of them as copies until you have a good grasp of virtual memory. Memory allocated by your process during its execution also comes from the heap.

    http://www.mikeash.com/pyblog/friday-qa-2011-12-16-disassembling-the-assembly-part-1.html
    breeswish
        8
    breeswish  
    OP
       2013-07-07 23:15:34 +08:00
    @lldong 感谢!很好奇你是用什么关键字找到这段的~求透露:D
    lldong
        9
    lldong  
       2013-07-08 10:00:15 +08:00
    @breeswish 是之前看过的文章
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5805 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 06:31 PVG 14:31 LAX 23:31 JFK 02:31
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86