简体   繁体   中英

How do I maintain stack in NASM using in Ubuntu11.04

For my project work, I have tested a code snippet. And I found that this code snippet arises segmentation fault due to the use of stack.Yet, I have to solve it using stack. Can any one help me to find out the problem? My program will display the second character of the string variable. My code snippet is following:

section.data
  string db "test",10,0
  msg2   db "%c",10,0
main:

xor eax,eax
mov eax, string
mov ebx,1    ; suppose I want to get the second character of string. SO I store the index of that character in ebx

pusha
push eax
push ebx
add esp,8
popa

pop dword[ebx]   ;  **I assume that this following 3 lines arise segmentation fault**   
pop eax          ;
mov bl,[eax+ebx] ; I want to move the fisrt character to BL using the index value stored in ebx which i popped just.

pusha
push ebx
call putchar
add esp,4
popa

pusha
push msg2
call printf
add esp,4
popa

Here, for your kind consideration I want to make it clear that the purpose of this code snippet is to know how to manipulate a stack.

Here, @nrz has recently given me a idea of the following code and I edit my above code here:

  section.data
    string db "test",10,0
    msg2   db "%c",10,0
  main:
    xor eax,eax
    mov eax, string
    mov ebx,1  ; suppose I want to get the second character of string. SO I store the index of that character in ebx
    mov   eax,string
    movzx eax,byte [eax]
    push  eax        ; these push and pop are for solving it using the stack,
    pop   ebx
    pusha
    push ebx
    call putchar
    add esp,4
    popa
    pusha
    push msg2
    call printf
    add esp,4
    popa

My query is specifcally:

  1. I will give the index value. Should it be in the ebx register?

  2. And above all my main idea of using stack is to acccess every character of string variable using the index value I pushed in ebx earlier. [It is mandatory. is it possible?]

  3. I want to store the output in a 8-bit register also.

    So my all in all idea is something like:

     mov al, [string+ebx] ;is it possible? 

    I have to take the value of ebx from the stack. I will put a value in ebx , then push ebx and at the time of mov al,[string+ebx] , I will pop ebx to get the value for mov instruction. More likely the instructions will be looked like:

      pop ebx mov al,[string+dword[ebx]] ;which is a wrong statement shown by NASM 

I am eagerly waiting for your response.

THank you,

There are some bugs and unnecessary instructions in the code:

mov eax, string  ; this line makes eax point to string, but...
xor eax,eax      ; this line zeroes eax immediately. delete this line.
mov ebx,1        ; this line is unnecessary, but does not cause problems.

pusha            ; unnecessary.
push eax         ; unnecessary.
push ebx         ; unnecessary.
add esp,8        ; unnecessary.
popa             ; unnecessary.

pop dword[ebx]   ; [1] = [esp], esp = esp+4, probably causes segmentation fault.
pop eax          ; eax = [esp], esp = esp+4
mov bl,[eax+ebx]

Edit: to print all the characters, just loop through the string:

section.data
  string db "test",10
  string_length equ $-string ; number of characters in the string.
                             ; no zero needed in the end of the string.
  msg2   db "%c",10,0
main:
    xor eax,eax      ; eax is the index to the string.

char_loop:
    push dword [eax+string]      ; push 4 first characters into stack.
    and  dword [esp], 0x000000ff ; x86 is little-endian, so 1st character
                                 ; is the lowest byte.
    pop  ebx                     ; pop the ASCII code of the character into ebx.

    pushad
    push ebx
    call putchar
    add esp,4
    popad

    inc eax
    cmp eax,string_length
    jb  char_loop

Edit: New code to match the changes in the question, using ebx as an index.

section.data
  string db "test",10
  string_length equ $-string ; number of characters in the string.
                             ; no zero needed in the end of the string.
  msg2   db "%c",10,0
main:
    xor  eax,eax             ; zero eax, for putchar.
    xor  ebx,ebx             ; ebx is the index to the string.

char_loop:
    ;push dword [ebx+string]      ; push 4 first characters into stack.
    ;and  dword [esp], 0x000000ff ; x86 is little-endian, so 1st character
    ;                             ; is the lowest byte.
    ;pop  eax                     ; pop the ASCII code of the character into ebx.

    mov   al,[ebx+string]         ; this is the normal way to do it.
                                  ; the above 3 instructions do it "using stack",
                                  ; as requested in the question.
    pushad
    push eax
    call putchar
    add esp,4
    popad

    inc ebx
    cmp ebx,string_length
    jb  char_loop

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