在汇编程序函数中获取char指针

时间:2012-05-02 16:37:07

标签: c linux assembly

我需要获取我的char指针,之后我将它打印在屏幕上,看看它是否正常工作。 我从文件中读取文本并放入我的char指针(sizeof(char *)+ filesize)+ 1 ..最后我把'\ 0'。 如果我打印我的char *它很好 这是我的asm代码

; void processdata(char* filecontent);
section .text
    global processdata
    extern printf
section .data
FORMAT: db '%c', 10, 0 ; to break the line 10, 0
processdata:
    push ebp
    mov ebp, esp
    mov ebx, [ebp]
    push ebx
    push FORMAT
    call printf
    add esp, 8

当我运行它时,我只看到变量中的垃圾。

紫罗兰说:

; void processdata(char* filecontent);
section .text
        [GLOBAL processdata] ;global processdata 
        extern printf
section .data
FORMAT: db '%c', 0
EQUAL: db "is equal", 10, 0
processdata:

    lea   esi,  [esp]
    mov   ebx,  FORMAT 
oook:   
    mov   eax,  [esi]
    push  eax
    push  ebx 
    call  printf
    inc   esi
    cmp  esi, 0x0
    jnz  oook

由于

2 个答案:

答案 0 :(得分:1)

引用demonofnight

  

但如果我需要增加,我可以这样做吗?

使用原始函数arg char*%c格式,如下所示:

    lea   esi,  [esp+4]
    mov   ebx,  FORMAT 
oook:   
    mov   eax,  [esi]
    push  eax
    push  ebx 
    call  printf
    inc   esi
    cmp  [esi], 0x0
    jnz  oook


[编辑:好的,我快速入侵了一些shenzi winOS inline __asm阻止了你 这是在linux和nasm中完成的一件事:

; ----------------------------------------------------------------------------
; blah.asm
; ----------------------------------------------------------------------------  

        extern  printf


        SECTION .data                   ;local variables
        fmt: db "next char:%c", 10, 0   ;printf format, "\n",'0'


        SECTION .text                   ;hack in some codes XD
        global foo

foo:                                    ;foo entry
        push  ebp                       ;set up stack frame
        mov   ebp,esp

        mov   esi, [ebp+8]              ;load the input char array to the source index

oook:                                   ;loop while we have some chars
        push  dword [esi]               ;push next char addy in string to stack
        push  dword fmt                 ;push stored format string to stack

        call  printf

        add   esp, 8                    ;restore stack pointer 
        inc   esi                       ;iterate to next char
        cmp   byte [esi], 0             ;test for null terminator byte 
        jnz   oook

        mov   esp, ebp                  ;restore stack frame
        pop   ebp

        mov   eax,0                     ;return 0
        ret                             ;done


blah.c(调用.asm foo):

/*-----------------------------------------------
blah.c
invokes some asm foo
------------------------------------------------*/
#include <stdio.h>

void foo(char*);

int main() {

    char sz[]={"oook\n"};
    foo(sz);

    return 0;
}


&amp;这是命令行的东西:

$ nasm -f elf blah.asm -o blah.o
$ gcc -o blah blah.c blah.o
$ ./blah
next char:o
next char:o
next char:o
next char:k
next char:

$
$ nasm -v
NASM version 2.09.08 compiled on Apr 30 2011

$ uname -a
Linux violet-313 3.0.0-17-generic #30-Ubuntu SMP Thu Mar 8 17:34:21 UTC 2012 i686 i686 i386 GNU/Linux


希望它适合你;)

答案 1 :(得分:0)

在linux x86上,第一个函数参数在ecx中。用于打印指针的printf格式为"%p"

所以

    ...
    FORMAT: db '%p', 10, 0 ; to break the line 10, 0
processdata:
    mov eax, [esp+4]
    push eax
    push format
    call printf
    add esp, 8
    ret

应该有效,假设你的其余代码是正确的并且你正在使用gcc的调用约定。

这假设你要打印的指针在堆栈上。

崩溃的原因可能是您在堆栈上推送了12个字节,但只将堆栈指针校正为8。