简体   繁体   中英

x86 Intel Assembly Linux sys_write + sys_read

The following code:

section        .bss

name:    resb 50

section        .text
global         _start

_start:
        PUSH   EBP
        MOV    EBP, ESP
        MOV    EDX, len
        MOV    ECX, msg
        MOV    EBX, 1
        MOV    EAX, 4
        INT    0x80
        MOV    EDX, 50
        MOV    ECX, name
        MOV    EBX, 0
        MOV    EAX, 3
        INT    0x80
        MOV    EBX, 1
        MOV    EAX, 4
        INT    0x80
        MOV    EDX, cm
        MOV    ECX, ex
        MOV    EBX, 1
        MOV    EAX, 4
        INT    0x80
        MOV    EBX, 0
        MOV    EAX, 1
        INT    0x80
section        .data

msg           db 'Hello!',0xa
ex            db '!',0xa
len           equ $ - msg
cm            equ $ - ex

I intended to make a simple I/O program that printed Hello! , asked for a char and would print %c! .

Input being | and output being : , I get the following:

:Hello!

:!

|4

:4

:!

How do I make it so that it returns the following

:Hello!

|4

:4!

As Damien_The_Unbeliever says, your equ s want to come immediately after the string they're supposed to measure. After your sys_read , eax will be the number of characters read, including the linefeed that ends the reading. You probably don't want to print the linefeed (in this case - sometimes you would). So:

mov edx, eax
dec edx

Or if you want to do it in one instruction:

lea edx, [eax - 1]

As it stands, edx still holds 50, so your next sys_write will print 50 characters. It will NOT stop at a zero or any other string-terminator. ecx will still contain name , but I would reload it just for clarity.

By rights, you should check for an error return ( eax would be negative) after each and every int 0x80 but an error is unlikely here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM