了解nasm程序中的列表文件

时间:2013-07-17 17:21:25

标签: nasm

我不明白为什么以下程序输出:356 它是如何与列表文件理解相关联的。 另一个问题,当我在第二行添加“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

感谢。

1 个答案:

答案 0 :(得分: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中修改自身时崩溃。