這是我第一次在這裏發佈。我不確定我是否正確地完成了格式化,所以請原諒我,如果我搞砸了。NASM - 分段故障和其他問題
無論如何,這應該需要兩個輸入,減半和加倍,然後打印出來。它不應該正常工作,因爲輸入數據是文字,但輸出仍然是令人困惑:
[poise] [/home/a/a_mccr/terminal] > ./assignment2
Please enter a four-digit number, negative or positive
1234
The number you entered is
Half the entered value is
ÞH
Double the entered value is
x# ÞH
Segmentation fault
它不會打印出我輸入(這是1234),然後每輸出時間PH和X#PH。所有這些都表明輸入沒有被存儲,但我無法弄清楚爲什麼。在程序結束時我也遇到了一個神祕的分段錯誤......幫助!下面的代碼:
segment .data ;to compile use: nasm -f elf assignment2.asm
; ld -o assignment2 assignment2.o
msg1 db 'Please enter a four-digit number, negative or positive', 0xA
len1 equ $-msg1 ;length of 1st message
msg2 db 'The number you entered is', 0xA
len2 equ $-msg2 ;length of 2nd message
msg3 db 'Half the entered value is', 0xA
len3 equ $-msg3 ;length of 3rd message
msg4 db 'Double the entered value is', 0xA
len4 equ $-msg4 ;length of 4th message
segment .bss
input2 resb 3 ;reserve 5 bytes for the entered number
input resb 3 ;reserve 5 bytes for the entered number
segment .text
global _start
_start:
mov eax, 4 ;select kernel call 4, the write function
mov ebx, 1 ;use the default output device (print in terminal)
mov ecx, msg1 ;set the pointer to msg
mov edx, len1 ;set the length to len
int 0x80 ;call write function
mov eax, 3 ;select the kernel read function
mov ebx, 0 ;use the default input device (user txt input)
mov ecx, input ;pointer to input variable
int 0x80 ;invoke kernel read function
mov eax, 4 ;select kernel call 4, the write function
mov ebx, 1 ;use the default output device (print in terminal)
mov ecx, msg2 ;set the pointer to msg2
mov edx, len2 ;set the length to len2
int 0x80 ;call write function
mov eax, 4 ;select kernel call 4, the write function
mov ebx, 1 ;use the default output device (print in terminal)
mov ecx, input ;set the pointer to input
int 0x80 ;call write function
mov eax, [input] ;move input to eax register
mov ebx, [input] ;move input to ebx register
shr eax, 1 ;shift eax 1 place to the right
shl ebx, 1 ;shift ebx 1 place to the left
mov [input], eax ;move contents of eax to input
mov [input2], ebx ;move contents of ebx to input2
mov eax, 4 ;Write message about half
mov ebx, 1 ;use the default output device (print in terminal)
mov ecx, msg3 ;set the pointer to msg3
mov edx, len3 ;set the length to len3
int 0x80 ;call write function
mov eax, 4 ;write contents of input
mov ebx, 1 ;use the default output device (print in terminal)
mov ecx, input ;set the pointer to input
int 0x80 ;call write function
mov eax, 4 ;write message about double
mov ebx, 1 ;use the default output device (print in terminal)
mov ecx, msg4 ;set the pointer to msg4
mov edx, len4 ;set the length to len4
int 0x80 ;call write function
mov eax, 4 ;write contents of input2
mov ebx, 1 ;use the default output device (print in terminal)
mov ecx, input2 ;set the pointer to input2
int 0x80 ;call write function
_exit:
mov eax, 1 ;standard exit
mov ebx, 0 ;0 is normal
int 0x80
有些評論不完全正確,因爲我有時在編輯一行後忘記更新它們。我實際上使用了3個字節,因爲我最多需要5個4位點(負號和4個整數)。我應該怎樣設定長度? – mavix 2012-02-16 02:56:10
哦,我想我不必告訴它的長度,如果它是一個實例變量(TA沒有在他給我們的示例代碼中) – mavix 2012-02-16 02:57:40
啊哈,三個字節是_definitely_不足以容納四個字節'1234 ' - 而且,自從我這樣做以來已經過了很多年了,但在您敲'^ D'結束輸入之前,可能需要'\ n'的空間。到目前爲止,它仍然是純文本。 – sarnold 2012-02-16 03:06:49