【发布时间】:2012-02-16 02:47:13
【问题描述】:
这是我第一次在这里发帖。我不确定我的格式是否正确,如果我搞砸了,请原谅我。
无论如何,这应该接受两个输入,一个减半,另一个加倍,然后打印它们。它不应该正常工作,因为输入数据是字符,但输出仍然令人困惑:
[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
【问题讨论】:
标签: assembly segmentation-fault nasm