liaoxl 发表于 2018-11-16 20:06:03

push 默认把 寄存器内容入栈是怎么回事。视频p30问提

本帖最后由 liaoxl 于 2018-11-16 20:08 编辑

因为push老是把cs ip这些寄存器的内容加入在栈顶的前面,循环到后面会覆盖前面的数据,导致结果不正确


难道是debug的问题?









代码如下

assume cs:code,ds:data,ss:stack
data segment
dw 0123h,0456h,0789h,0abch,0defh,0fedh,0ebah,0987h
data ends
stack segment
dw 0,0,0,0,0,0,0,0
stack ends
code segment
start:        mov ax,stack
                mov ss,ax
                mov sp,16
                mov ax,data
                mov ds,ax
                mov bx,0
                mov cx,8
                s:push
                add bx,2
                loop s
               
                mov bx,0
                mov cx,8
                s0:pop
                add bx,2
                loop s0
               
                mov ax,4c00h
                int 21h
code ends
end start

liaoxl 发表于 2018-11-17 10:40:10

应该多分配16字节的空间做栈的前缀吗?
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
不然会随着入栈会覆盖掉
dw 0123h,0456h,0789h,0abch,0defh,0fedh,0ebah,0987h
这里面的数据,如果真有这回事,那教程里的程序应该不会这样写,是我的调试环境导致的吗?

liaoxl 发表于 2018-11-17 14:13:48

百度找到的答案,"debug载入并运行程式时,下达t,p或g等指令时,会短暂的借主程式的栈以作暂存之用.比如 [保存] 主程式的cs,ip或flag等值",看来程序本身没问题,是debug的问题。在后面的视频里,小甲鱼调试的时候我也看到类似的结果,修改ss和sp时会改变这一段内存。{:10_249:}

水柔炎 发表于 2018-11-18 17:23:05

你注意push时,先指针减一,再入栈,pop时,先读内容,然后指针加一

liaoxl 发表于 2018-11-19 08:12:02

本帖最后由 liaoxl 于 2018-11-19 08:13 编辑

水柔炎 发表于 2018-11-18 17:23
你注意push时,先指针减一,再入栈,pop时,先读内容,然后指针加一

push 和pop指令导致sp的变动我知道,这里的问题是执行mov ss,ax,debug会往ss:sp之前的空间写入当前寄存器的值,执行mov sp,idata也会,执行push ax也会,执行push的时候如果栈的空间太小,debug写入的数据会覆盖掉前面的数据,导致结果错误…其实我也不确定是不是debug做的,刚学没多久,因为不用的debug就看不到内存的变化,用了debug看,可能就是debug改变了内存,测不准原理???

水柔炎 发表于 2018-11-19 12:06:28

liaoxl 发表于 2018-11-19 08:12
push 和pop指令导致sp的变动我知道,这里的问题是执行mov ss,ax,debug会往ss:sp之前的空间写入当前寄 ...

问问老师咯

人造人 发表于 2018-11-19 23:49:40

liaoxl 发表于 2018-11-19 08:12
push 和pop指令导致sp的变动我知道,这里的问题是执行mov ss,ax,debug会往ss:sp之前的空间写入当前寄 ...

测不准原理?
什么是测不准原理?
测不准的根本原因是因为你的测量工具不行


        jmp        0x07c0: start
start:
        mov        bx, 16
       
        xor        dx, dx
        mov        ax, 0x7c00 + section.stack.start
        div        bx
        mov        ss, ax
        mov        sp, 16
        xor        dx, dx
        mov        ax, 0x7c00 + section.data.start
        div        bx
        mov        ds, ax
       
        mov        bx, 0
        mov        cx, 8
s:
        push        word
        add        bx, 2
        loop        s
       
        mov        bx, 0
        mov        cx, 8
s0:
        pop        word
        add        bx, 2
        loop        s0
.L:
        cli
        hlt
        jmp        .L
       

        dw 0123h, 0456h, 0789h, 0abch, 0defh, 0fedh, 0ebah, 0987h
       

        dw 0, 0, 0, 0, 0, 0, 0, 0


下面是调试记录

E:\tmp\asm>make debug
bochsdbg -q -f bochsrc.bxrc
========================================================================
                     Bochs x86 Emulator 2.6.9
               Built from SVN snapshot on April 9, 2017
                  Compiled on Apr9 2017 at 09:49:25
========================================================================
00000000000i[      ] reading configuration from bochsrc.bxrc
00000000000i[      ] installing win32 module as the Bochs GUI
00000000000i[      ] using log file bochsout.txt
Next at t=0
(0) f000:fff0 (unk. ctxt): jmpf 0xf000:e05b          ; ea5be000f0
<bochs:1> b 0x7c00
<bochs:2> c
(0) Breakpoint 1, 0x0000000000007c00 in ?? ()
Next at t=60706119
(0) 0000:7c00 (unk. ctxt): jmpf 0x07c0:0005          ; ea0500c007
<bochs:3> u/30
00007c00: (                  ): jmpf 0x07c0:0005          ; ea0500c007
00007c05: (                  ): mov bx, 0x0010            ; bb1000
00007c08: (                  ): xor dx, dx                ; 31d2
00007c0a: (                  ): mov ax, 0x7c50            ; b8507c
00007c0d: (                  ): div ax, bx                ; f7f3
00007c0f: (                  ): mov ss, ax                ; 8ed0
00007c11: (                  ): mov sp, 0x0010            ; bc1000
00007c14: (                  ): xor dx, dx                ; 31d2
00007c16: (                  ): mov ax, 0x7c40            ; b8407c
00007c19: (                  ): div ax, bx                ; f7f3
00007c1b: (                  ): mov ds, ax                ; 8ed8
00007c1d: (                  ): mov bx, 0x0000            ; bb0000
00007c20: (                  ): mov cx, 0x0008            ; b90800
00007c23: (                  ): push word ptr ds:   ; ff37
00007c25: (                  ): add bx, 0x0002            ; 83c302
00007c28: (                  ): loop .-7                  ; e2f9
00007c2a: (                  ): mov bx, 0x0000            ; bb0000
00007c2d: (                  ): mov cx, 0x0008            ; b90800
00007c30: (                  ): pop word ptr ds:      ; 8f07
00007c32: (                  ): add bx, 0x0002            ; 83c302
00007c35: (                  ): loop .-7                  ; e2f9
00007c37: (                  ): cli                     ; fa
00007c38: (                  ): hlt                     ; f4
00007c39: (                  ): jmp .-4                   ; ebfc
00007c3b: (                  ): add byte ptr ds:, al ; 0000
00007c3d: (                  ): add byte ptr ds:, al ; 0000
00007c3f: (                  ): add byte ptr ss:, ah ; 0023
00007c41: (                  ): add word ptr ss:, dx ; 015604
00007c44: (                  ): mov word ptr ds:, ax; 8907
00007c46: (                  ): mov sp, 0xef0a            ; bc0aef
<bochs:4> s
Next at t=60706120
(0) 07c0:0005 (unk. ctxt): mov bx, 0x0010            ; bb1000
<bochs:5>
Next at t=60706121
(0) 07c0:0008 (unk. ctxt): xor dx, dx                ; 31d2
<bochs:6>
Next at t=60706122
(0) 07c0:000a (unk. ctxt): mov ax, 0x7c50            ; b8507c
<bochs:7>
Next at t=60706123
(0) 07c0:000d (unk. ctxt): div ax, bx                ; f7f3
<bochs:8>
Next at t=60706124
(0) 07c0:000f (unk. ctxt): mov ss, ax                ; 8ed0
<bochs:9>
Next at t=60706125
(0) 07c0:0011 (unk. ctxt): mov sp, 0x0010            ; bc1000
<bochs:10>
Next at t=60706126
(0) 07c0:0014 (unk. ctxt): xor dx, dx                ; 31d2
<bochs:11>
Next at t=60706127
(0) 07c0:0016 (unk. ctxt): mov ax, 0x7c40            ; b8407c
<bochs:12>
Next at t=60706128
(0) 07c0:0019 (unk. ctxt): div ax, bx                ; f7f3
<bochs:13>
Next at t=60706129
(0) 07c0:001b (unk. ctxt): mov ds, ax                ; 8ed8
<bochs:14>
Next at t=60706130
(0) 07c0:001d (unk. ctxt): mov bx, 0x0000            ; bb0000
<bochs:15> sreg
es:0x0000, dh=0x00009300, dl=0x0000ffff, valid=1
      Data segment, base=0x00000000, limit=0x0000ffff, Read/Write, Accessed
cs:0x07c0, dh=0x00009300, dl=0x7c00ffff, valid=1
      Data segment, base=0x00007c00, limit=0x0000ffff, Read/Write, Accessed
ss:0x07c5, dh=0x00009300, dl=0x7c50ffff, valid=1
      Data segment, base=0x00007c50, limit=0x0000ffff, Read/Write, Accessed
ds:0x07c4, dh=0x00009300, dl=0x7c40ffff, valid=1
      Data segment, base=0x00007c40, limit=0x0000ffff, Read/Write, Accessed
fs:0x0000, dh=0x00009300, dl=0x0000ffff, valid=1
      Data segment, base=0x00000000, limit=0x0000ffff, Read/Write, Accessed
gs:0x0000, dh=0x00009300, dl=0x0000ffff, valid=1
      Data segment, base=0x00000000, limit=0x0000ffff, Read/Write, Accessed
ldtr:0x0000, dh=0x00008200, dl=0x0000ffff, valid=1
tr:0x0000, dh=0x00008b00, dl=0x0000ffff, valid=1
gdtr:base=0x00000000000f9a37, limit=0x30
idtr:base=0x0000000000000000, limit=0x3ff
<bochs:16> x/8hx 0x7c40
:
0x0000000000007c40 <bogus+       0>:    0x01230x04560x07890x0abc0x0def0x0fed0x0eba0x0987
<bochs:17> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x00000x00000x00000x00000x00000x00000x00000x0000
<bochs:18> s
Next at t=60706131
(0) 07c0:0020 (unk. ctxt): mov cx, 0x0008            ; b90800
<bochs:19>
Next at t=60706132
(0) 07c0:0023 (unk. ctxt): push word ptr ds:   ; ff37
<bochs:20> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x00000x00000x00000x00000x00000x00000x00000x0000
<bochs:21> s
Next at t=60706133
(0) 07c0:0025 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:22> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x00000x00000x00000x00000x00000x00000x00000x0123
<bochs:23> s
Next at t=60706134
(0) 07c0:0028 (unk. ctxt): loop .-7 (0x00007c23)   ; e2f9
<bochs:24>
Next at t=60706135
(0) 07c0:0023 (unk. ctxt): push word ptr ds:   ; ff37
<bochs:25>
Next at t=60706136
(0) 07c0:0025 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:26> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x00000x00000x00000x00000x00000x00000x04560x0123
<bochs:27> s
Next at t=60706137
(0) 07c0:0028 (unk. ctxt): loop .-7 (0x00007c23)   ; e2f9
<bochs:28>
Next at t=60706138
(0) 07c0:0023 (unk. ctxt): push word ptr ds:   ; ff37
<bochs:29>
Next at t=60706139
(0) 07c0:0025 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:30> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x00000x00000x00000x00000x00000x07890x04560x0123
<bochs:31> s
Next at t=60706140
(0) 07c0:0028 (unk. ctxt): loop .-7 (0x00007c23)   ; e2f9
<bochs:32>
Next at t=60706141
(0) 07c0:0023 (unk. ctxt): push word ptr ds:   ; ff37
<bochs:33>
Next at t=60706142
(0) 07c0:0025 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:34> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x00000x00000x00000x00000x0abc0x07890x04560x0123
<bochs:35> s
Next at t=60706143
(0) 07c0:0028 (unk. ctxt): loop .-7 (0x00007c23)   ; e2f9
<bochs:36>
Next at t=60706144
(0) 07c0:0023 (unk. ctxt): push word ptr ds:   ; ff37
<bochs:37>
Next at t=60706145
(0) 07c0:0025 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:38> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x00000x00000x00000x0def0x0abc0x07890x04560x0123
<bochs:39> s
Next at t=60706146
(0) 07c0:0028 (unk. ctxt): loop .-7 (0x00007c23)   ; e2f9
<bochs:40>
Next at t=60706147
(0) 07c0:0023 (unk. ctxt): push word ptr ds:   ; ff37
<bochs:41>
Next at t=60706148
(0) 07c0:0025 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:42> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x00000x00000x0fed0x0def0x0abc0x07890x04560x0123
<bochs:43> s
Next at t=60706149
(0) 07c0:0028 (unk. ctxt): loop .-7 (0x00007c23)   ; e2f9
<bochs:44>
Next at t=60706150
(0) 07c0:0023 (unk. ctxt): push word ptr ds:   ; ff37
<bochs:45>
Next at t=60706151
(0) 07c0:0025 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:46> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x00000x0eba0x0fed0x0def0x0abc0x07890x04560x0123
<bochs:47> s
Next at t=60706152
(0) 07c0:0028 (unk. ctxt): loop .-7 (0x00007c23)   ; e2f9
<bochs:48>
Next at t=60706153
(0) 07c0:0023 (unk. ctxt): push word ptr ds:   ; ff37
<bochs:49>
Next at t=60706154
(0) 07c0:0025 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:50> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x04560x0123
<bochs:51> s
Next at t=60706155
(0) 07c0:0028 (unk. ctxt): loop .-7 (0x00007c23)   ; e2f9
<bochs:52>
Next at t=60706156
(0) 07c0:002a (unk. ctxt): mov bx, 0x0000            ; bb0000
<bochs:53> x/8hx 0x7c40
:
0x0000000000007c40 <bogus+       0>:    0x01230x04560x07890x0abc0x0def0x0fed0x0eba0x0987
<bochs:54> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x04560x0123
<bochs:55> sreg
es:0x0000, dh=0x00009300, dl=0x0000ffff, valid=1
      Data segment, base=0x00000000, limit=0x0000ffff, Read/Write, Accessed
cs:0x07c0, dh=0x00009300, dl=0x7c00ffff, valid=1
      Data segment, base=0x00007c00, limit=0x0000ffff, Read/Write, Accessed
ss:0x07c5, dh=0x00009300, dl=0x7c50ffff, valid=7
      Data segment, base=0x00007c50, limit=0x0000ffff, Read/Write, Accessed
ds:0x07c4, dh=0x00009300, dl=0x7c40ffff, valid=3
      Data segment, base=0x00007c40, limit=0x0000ffff, Read/Write, Accessed
fs:0x0000, dh=0x00009300, dl=0x0000ffff, valid=1
      Data segment, base=0x00000000, limit=0x0000ffff, Read/Write, Accessed
gs:0x0000, dh=0x00009300, dl=0x0000ffff, valid=1
      Data segment, base=0x00000000, limit=0x0000ffff, Read/Write, Accessed
ldtr:0x0000, dh=0x00008200, dl=0x0000ffff, valid=1
tr:0x0000, dh=0x00008b00, dl=0x0000ffff, valid=1
gdtr:base=0x00000000000f9a37, limit=0x30
idtr:base=0x0000000000000000, limit=0x3ff
<bochs:56> s
Next at t=60706157
(0) 07c0:002d (unk. ctxt): mov cx, 0x0008            ; b90800
<bochs:57>
Next at t=60706158
(0) 07c0:0030 (unk. ctxt): pop word ptr ds:      ; 8f07
<bochs:58>
Next at t=60706159
(0) 07c0:0032 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:59> x/8hx 0x7c40
:
0x0000000000007c40 <bogus+       0>:    0x09870x04560x07890x0abc0x0def0x0fed0x0eba0x0987
<bochs:60> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x04560x0123
<bochs:61> s
Next at t=60706160
(0) 07c0:0035 (unk. ctxt): loop .-7 (0x00007c30)   ; e2f9
<bochs:62>
Next at t=60706161
(0) 07c0:0030 (unk. ctxt): pop word ptr ds:      ; 8f07
<bochs:63>
Next at t=60706162
(0) 07c0:0032 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:64> x/8hx 0x7c40
:
0x0000000000007c40 <bogus+       0>:    0x09870x0eba0x07890x0abc0x0def0x0fed0x0eba0x0987
<bochs:65> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x04560x0123
<bochs:66> s
Next at t=60706163
(0) 07c0:0035 (unk. ctxt): loop .-7 (0x00007c30)   ; e2f9
<bochs:67>
Next at t=60706164
(0) 07c0:0030 (unk. ctxt): pop word ptr ds:      ; 8f07
<bochs:68>
Next at t=60706165
(0) 07c0:0032 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:69> x/8hx 0x7c40
:
0x0000000000007c40 <bogus+       0>:    0x09870x0eba0x0fed0x0abc0x0def0x0fed0x0eba0x0987
<bochs:70> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x04560x0123
<bochs:71> s
Next at t=60706166
(0) 07c0:0035 (unk. ctxt): loop .-7 (0x00007c30)   ; e2f9
<bochs:72>
Next at t=60706167
(0) 07c0:0030 (unk. ctxt): pop word ptr ds:      ; 8f07
<bochs:73>
Next at t=60706168
(0) 07c0:0032 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:74> x/8hx 0x7c40
:
0x0000000000007c40 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0def0x0fed0x0eba0x0987
<bochs:75> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x04560x0123
<bochs:76> s
Next at t=60706169
(0) 07c0:0035 (unk. ctxt): loop .-7 (0x00007c30)   ; e2f9
<bochs:77>
Next at t=60706170
(0) 07c0:0030 (unk. ctxt): pop word ptr ds:      ; 8f07
<bochs:78>
Next at t=60706171
(0) 07c0:0032 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:79> x/8hx 0x7c40
:
0x0000000000007c40 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x0fed0x0eba0x0987
<bochs:80> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x04560x0123
<bochs:81> s
Next at t=60706172
(0) 07c0:0035 (unk. ctxt): loop .-7 (0x00007c30)   ; e2f9
<bochs:82>
Next at t=60706173
(0) 07c0:0030 (unk. ctxt): pop word ptr ds:      ; 8f07
<bochs:83>
Next at t=60706174
(0) 07c0:0032 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:84> x/8hx 0x7c40
:
0x0000000000007c40 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x0eba0x0987
<bochs:85> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x04560x0123
<bochs:86> s
Next at t=60706175
(0) 07c0:0035 (unk. ctxt): loop .-7 (0x00007c30)   ; e2f9
<bochs:87>
Next at t=60706176
(0) 07c0:0030 (unk. ctxt): pop word ptr ds:      ; 8f07
<bochs:88>
Next at t=60706177
(0) 07c0:0032 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:89> x/8hx 0x7c40
:
0x0000000000007c40 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x04560x0987
<bochs:90> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x04560x0123
<bochs:91> s
Next at t=60706178
(0) 07c0:0035 (unk. ctxt): loop .-7 (0x00007c30)   ; e2f9
<bochs:92>
Next at t=60706179
(0) 07c0:0030 (unk. ctxt): pop word ptr ds:      ; 8f07
<bochs:93>
Next at t=60706180
(0) 07c0:0032 (unk. ctxt): add bx, 0x0002            ; 83c302
<bochs:94> x/8hx 0x7c40
:
0x0000000000007c40 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x04560x0123
<bochs:95> x/8hx 0x7c50
:
0x0000000000007c50 <bogus+       0>:    0x09870x0eba0x0fed0x0def0x0abc0x07890x04560x0123
<bochs:96> s
Next at t=60706181
(0) 07c0:0035 (unk. ctxt): loop .-7 (0x00007c30)   ; e2f9
<bochs:97>
Next at t=60706182
(0) 07c0:0037 (unk. ctxt): cli                     ; fa
<bochs:98>
Next at t=60706183
(0) 07c0:0038 (unk. ctxt): hlt                     ; f4
<bochs:99>
Next at t=60706184
(0) 07c0:0039 (unk. ctxt): jmp .-4 (0x00007c37)      ; ebfc
<bochs:100>




看不懂?
没关系,继续往后学吧,后面的内容更精彩
^_^

HOHD 发表于 2018-11-20 11:10:08

人造人 发表于 2018-11-19 23:49
测不准原理?
什么是测不准原理?
测不准的根本原因是因为你的测量工具不行


哥们可以

卡兔压 发表于 2019-1-19 19:12:11

人造人 发表于 2018-11-19 23:49
测不准原理?
什么是测不准原理?
测不准的根本原因是因为你的测量工具不行


尼玛看不懂,先丢后面吧,慢慢学,写多了应该会理解
页: [1]
查看完整版本: push 默认把 寄存器内容入栈是怎么回事。视频p30问提