2013-07-17 34 views
0

我不明白爲什麼下面的程序輸出:356 它是如何連接到列表文件的理解。 另一個問題,爲什麼當我在第二行添加「section .text」時出現了段錯誤?瞭解在nasm程序中列出fie

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
; 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 

至於分段故障; 部分可能是隻讀的,並且這會導致程序在嘗試修改自己的指令7時崩潰。

+0

非常感謝!你能解釋一下嗎?'7 0000000A 000D [16000000]'中的方括號意味着什麼。如果我理解正確,那麼16000000是內存中的一個地址,從中可以讀取並隨後寫入** add **的參數。是否正確地說[]意味着地址不是最終的,並且將在鏈接器的重定位階段被修復? – user1264304

+1

@ user1264304:是的,它看起來像我。我不確定是否正確地說括號的存在意味着地址不是最終的。但是,括號用來顯示內存操作數。 – Michael