簡體   English   中英

在Linux中使用匯編將變量輸出到命令行

[英]Printing variable to command line using assembly in Linux

嘗試使用Linux匯編,我遇到了以下問題。 我剛開始,所以我的程序是一個相對簡單的程序,源於我在linuxassembly中發現的一些示例。 它采用傳遞給命令行的第一個參數並將其輸出。 這是我到目前為止所擁有的...

section .bss
    test_string: resb 3

section .text
    global _start

_start:
    pop ebx     ;argument number
    pop ebx     ;program name
    pop ebx     ;first argument
    mov [test_string],ebx

    mov eax,4
    mov ebx,1
    mov ecx,test_string
    mov edx,3
    int 80h

    mov eax,1
    mov ebx,0
    int 80h

我知道這寫得不好,但是由於我是新手,所以我只是想在繼續學習之前更好地理解匯編指令/變量的工作方式。 我用...組裝和鏈接

nasm -f elf first.asm
ld -m elf_i386 -s -o first first.o

然后我運行使用..

./first one two

我以為它會打印出one但它會打印出像Y*&這樣的亂七八糟的東西。 我究竟做錯了什么? 我的test_string類型錯誤嗎?

您正在嘗試打印出指向字符串的指針的值,而不是打印字符串。 您要改為執行此操作。

pop ebx     ;argument number
pop ebx     ;program name
pop ebx     ;pointer to the first argument

mov ecx,ebx ;load the pointer into ecx for the write system call

mov eax,4   ;load the other registers for the write system call
mov ebx,1
mov edx,3
int 80h

mov eax,1
mov ebx,0
int 80h

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM