0

次のプログラムが出力される理由がわかりません。別の質問ですが、2 行目に「section .text」を追加するとセグメンテーション違反が発生するのはなぜですか?

 1                                   global _start
 2                                  
 3                                   section .data
 4 00000000 03000000                x: dd 3
 5                                   
 6 00000004 8B0D[00000000]           _start: mov ecx, [x]
 7 0000000A 000D[16000000]          r: add byte [l+6], cl
 8 00000010 C605[00000000]30        l: mov byte [x], 48
 9 00000017 51                      push ecx
10 00000018 B804000000              mov eax,4
11 0000001D BB01000000              mov ebx, 1
12 00000022 B9[00000000]            mov ecx, x
13 00000027 BA01000000              mov edx,1
14 0000002C CD80                    int 0x80
15 0000002E 59                      pop ecx
16 0000002F E2D9                    loop r,ecx
17 00000031 BB00000000              mov ebx,0
18 00000036 B801000000              mov eax,1
19 0000003B CD80                    int 0x80

ありがとう。

4

1 に答える 1

1
; Set ecx=3
6 00000004 8B0D[00000000]           _start: mov ecx, [x]

; Adds cl to the low byte of the operand of instruction 8. So on the first
; iteration when ecx==3, it will add 3 to 48, resulting in 51, which is the
; ASCII code for the letter '3'.
; On the second iteration it will add 2, resulting in 51+2 = 53 = '5'.
; On the third iteration it will add 1, resulting in 53+1 = 54 = '6'
7 0000000A 000D[16000000]          r: add byte [l+6], cl
8 00000010 C605[00000000]30        l: mov byte [x], 48

; This code prints whatever is at x as if it was a string.
; Only the first character is printed (since edx==1).
; As explained above, on the first iteration of the loop x will
; contain the dword 0x00000033, on the second 0x00000035 and on
; the third 0x00000036. Since we're only printing one character (the
; least significant byte of the dword) on each iteration, we end up
; printing the characters 0x33, 0x35 and 0x36, which correspond to
; '3', '5' and '6' in ASCII.
9 00000017 51                      push ecx 
10 00000018 B804000000              mov eax,4
11 0000001D BB01000000              mov ebx, 1
12 00000022 B9[00000000]            mov ecx, x
13 00000027 BA01000000              mov edx,1
14 0000002C CD80                    int 0x80
15 0000002E 59                      pop ecx

; Decrease ecx by 1 and jump to r if ecx!=0
16 0000002F E2D9                    loop r,ecx

セグメンテーション違反については; セクションが読み取り専用になっている可能性が.textあり、これにより、命令 7 でプログラム自体を変更しようとすると、プログラムがクラッシュする可能性があります。

于 2013-07-17T17:42:27.737 に答える