Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 我的汇编程序中的分段错误_Linux_Assembly_X86_Nasm - Fatal编程技术网

Linux 我的汇编程序中的分段错误

Linux 我的汇编程序中的分段错误,linux,assembly,x86,nasm,Linux,Assembly,X86,Nasm,我从这个简单的启动程序中得到了一个分段错误 我正在使用Ubuntu 16.10和kdbg进行调试。到达starting\uuuu int 80h\uuuu后,停止移动到下一行 section .bss ; section containing uninitialized data BUFFLEN equ 1024 ; length of buffer Buff: resb BUFFLEN ; text buffer itself section .data ; sectio

我从这个简单的启动程序中得到了一个分段错误

我正在使用Ubuntu 16.10和
kdbg
进行调试。到达starting
\uuuu int 80h\uuuu
后,停止移动到下一行

section .bss    ; section containing uninitialized data

BUFFLEN equ 1024    ; length of buffer
Buff: resb BUFFLEN  ; text buffer itself

section .data   ; section containing initialzed data
section .text   ; secttion containing code

global _start   ; linker needs to find the entry point!

_start:
  nop       ; this no-op keeps gdb happy

  ; read buffer full of text form stdin:
read:
    mov eax, 3  ; specify sys_read call
    mov ebx, 0  ; specify file descriptor 0 : standard input
    mov ecx, Buff ; pass offset of the buffer to read to 
    mov edx, BUFFLEN    ;  pass number of bytes to be read at one pass
    int 80h     ; call sys_read to fill the buffer
    mov esi,eax     ; copy sys_read return value for safekeeping
    cmp eax, 0   ; if eax = 0 , sys_read reached EOF on stdin
    je Done     ; jump if Equal ( to o, form compare)


; set up the register for the process buffer step: 
    mov ecx, esi    ; place the number of bytes read into ecx
    mov ebp, Buff   ; pace address of buffer into ebp
    dec ebp         ; adjust the count to offset

; go through the buffer and cnvert lowercase to uppercase characters:
Scan:
    cmp byte [ebp+ecx], 61h     ; test input char agaisnst lowercase 'a'
    jb Next         ; if Below 'a'  in ASCII, not lowercase
    cmp byte [ebp+ecx], 7Ah     ; test against lowercase 'z'
    ja Next

    sub byte [ebx+ecx], 20h ; subtract 20h to give uppercase..

Next:
    dec ecx ; Decrement counter
    jnz Scan    ; if characters reamin, loop back

; Write the buffer full of processed text to stdout:
Write:
    mov eax,4   ; Specify sys_write call
    mov ebx, 1  ; Specify file descriptor 1 : stdout
    mov ecx, Buff   ; pass the offset of the buffer
    mov edx, esi ; pass the # of bytes of data in the buffer
    int 80h     ; make sys_write kernel call
    jmp read    ; loop back and load another buffer full

Done:
    mov eax, 1  ; Code for Exit sys_call
    mov ebx, 0  ; return code of Zero
    int 80h
我使用了以下命令:

nasm -f elf -g -F stabs uppercaser1.asm
ld -m elf_i386 -o uppercaser1 uppercaser1.o
./uppercaser < inputflie
nasm-f elf-g-f stab uppercase1.asm
ld-m elf_i386-o大写字母1大写字母1.o
/大写字母
我认为这段代码通常是公开的,所以我在发布时就考虑到了这一点

您应该使用代码作为指南来理解代码中可能存在的错误;然而,它不符合任何编码标准,因此仅仅复制、粘贴并将其作为作业提交是毫无意义的;假设是这样

您永远不会仅仅通过复制/粘贴来提高汇编编程技能

lsb_发布-a ... 说明:Ubuntu 16.04.3 LTS

nasm-f elf32-g大写1.s-o大写1.o&&ld-m elf_i386大写1.o-o大写1

section .bss
        Buff resb 1

section .data

section .text
        global _start
_start:
        nop

Read:
        mov eax, 3              ; read syscall
        mov ebx, 0              ; stdin
        mov ecx, Buff           ; pass address of the buffer to read to
        mov edx, 1              ; read one char or one byte
        int 0x80                ;

        cmp eax, 0              ; if syscall returns returns 0
        je Exit                 ;

        cmp byte [Buff], 0x61   ; lower case a
        jb Write                ; jump if byte is below a in ASCII chart
        cmp byte [Buff], 0x7a   ; lower case z
        ja Write                ; jump if byte is above z in ASCII chart

        sub byte [Buff], 0x20   ; changes the value in the buffer to an uppercase char


Write:
        mov eax, 4              ; write syscall
        mov ebx, 1              ; stdout
        mov ecx, Buff           ; what to print
        mov edx, 1              ; length is one byte - each char is a byte
        int 0x80
        jmp Read                ; go back to Read

Exit:
        mov eax, 1
        mov ebx, 0
        int 0x80
样本输出:

david@ubuntuserver00A:~/asm$ ./uppercase1  < uppercase1.s
SECTION .BSS
        BUFF RESB 1

SECTION .DATA

SECTION .TEXT
        GLOBAL _START
_START:
        NOP

READ:
        MOV EAX, 3              ; READ SYSCALL
        MOV EBX, 0              ; STDIN
        MOV ECX, BUFF           ; PASS ADDRESS OF THE BUFFER TO READ TO
        MOV EDX, 1              ; READ ONE CHAR OR ONE BYTE
        INT 0X80                ;

        CMP EAX, 0              ; IF SYSCALL RETURNS RETURNS 0
        JE EXIT                 ;

        CMP BYTE [BUFF], 0X61   ; LOWER CASE A
        JB WRITE                ; JUMP IF BYTE IS BELOW A IN ASCII CHART
        CMP BYTE [BUFF], 0X7A   ; LOWER CASE Z
        JA WRITE                ; JUMP IF BYTE IS ABOVE Z IN ASCII CHART

        SUB BYTE [BUFF], 0X20   ; CHANGES THE VALUE IN THE BUFFER TO AN UPPERCASE CHAR


WRITE:
        MOV EAX, 4              ; WRITE SYSCALL
        MOV EBX, 1              ; STDOUT
        MOV ECX, BUFF           ; WHAT TO PRINT
        MOV EDX, 1              ; LENGTH IS ONE BYTE - EACH CHAR IS A BYTE
        INT 0X80
        JMP READ                ; GO BACK TO READ

EXIT:
        MOV EAX, 1
        MOV EBX, 0
        INT 0X80
david@ubuntuserver00A:~/asm$。/uppercase1
您是否真的遇到了分段错误,或者该错误是否继续存在(即,它看起来好像挂起了)?这是一个巨大的区别。我建议您通过调试器运行代码。它应该告诉你在你的代码中这个崩溃的地方。使用
gdb./uppercaser1
例如,您使用
子字节[ebx+ecx],20h
。我想您的意思是使用
子字节[ebp+ecx],20h
?您是否尝试在不定向外部文件的情况下运行(例如,./uppercaser