I'm trying to copy a string to another and I receive the message "Segmentation fault"

I did this code following indications from my teacher but causes me the error "Segmentation fault"
Can you help me, please?

I have a string that I do not know the length, and I have to copy it to another variable:

mov eax, dinamicString
mov ,eax

mov esi, dinamicString
mov edi, destinationString
mov ecx, stringlenght

ciclo:
lodsb
stosb
loopnz ciclo
Posted on 2008-02-10 00:33:55 by nmpsantos
I just wrote this function for you. Take a close look at the instructions and post back if you have problems understanding them.

; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧
  __SegmentedCopyString:
    ; DWORD __SegmentedCopyString (DWORD SourceSegment, SourceOffset, DestinationSegment, DestinationOffset); StdCall;

    ; Returns the number of bytes copied to the destination string (Including the null-terminator)
    ; Returns zero upon any of the below conditions:
    ;  You don't have read permission to the segment selector
    ;  You don't have write permission to the segment selector
    ;  has its high order WORD set to some value
    ;  is NULL
    ;  has its high order WORD set to some value
    ;  is NULL


    PUSH    EBX
    PUSH    ECX
    PUSH    EDX
    PUSH    ESI
    PUSH    EDI
    PUSH    EBP
    MOV    EBP , ESP


    ; = SourceSegment
    ; = SourceOffset
    ; = DestinationSegment
    ; = DestinationOffset

    ; Predict Failure
    XOR    EAX , EAX
    XOR    EBX , EBX
    XOR    ECX , ECX
    MOV    ESI , DWORD PTR ; SourceSegment
    MOV    EDI , DWORD PTR ; DestinationSegment
    MOV    EDX , ESI
    OR      EDX , EDI
    TEST    EDX , 0xFFFF0000
    SETNZ  BL
    VERR    SI
    SETNZ  CL
    OR      EBX , ECX
    VERW    DI
    SETNZ  CL
    OR      EBX , ECX
    MOV    EDX , DWORD PTR ; SourceOffset
    TEST    EDX , EDX
    SETZ    CL
    OR      EBX , ECX
    MOV    EDX , DWORD PTR ; DestinationOffset
    TEST    EDX , EDX
    SETZ    CL
    OR      EBX , ECX
    JNZ    .EP


    ; ESI = SourceSegment
    ; EDI = DestinationSegment

    PUSH    DS
    PUSH    ES
    MOV    DS , ESI
    MOV    ES , EDI
    MOV    ESI , DWORD PTR ; SourceOffset
    MOV    EDI , DWORD PTR ; DestinationOffset
    MOV    EDX , 0x000000FF
    XOR    ECX , ECX
    .Loop:
      MOV    EAX , DWORD PTR
      INC    ESI
      INC    ECX
      AND    EAX , EDX
      JZ      .EndLoop
      MOV    DWORD PTR , EAX
      INC    EDI
      JMP    .Loop
    .EndLoop:
      POP    ES
      POP    DS
    .SetResult:
      MOV    EAX , ECX

    .EP:
      POP    EBP
      POP    EDI
      POP    ESI
      POP    EDX
      POP    ECX
      POP    EBX
    RET    (4*0x04)
; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧


The code is written in NASM. Oh Gosh! I'm so talented :shock:
Posted on 2008-02-10 04:29:06 by XCHG
My good............
It's to complex to me, I think!

There have a lot of many instructions that I have never seen in class.

I'm completely disoriented!
This is what I am doing:


      .
      .
      .
mov esi, data
mov edi, currentLine
mov edx, 0x000000FF
loop1:
mov ebx, 0x0000000A
mov eax, DWORD
and eax, edx
inc esi
cmp eax, ebx ;verifica se o caracter 0xA
je proccessline

mov DWORD , eax
inc edi
jmp loop1

proccessline:

mov eax,
add eax, 1
mov , eax

mov ebx, lineCount
mov al, byte
cmp al, 1
je assignLine1

mov ebx, lineCount
mov al, byte
cmp al, 2
je assignLine2

mov ebx, lineCount
mov al, byte
cmp al, 3
je assignLine3


jmp fim

assignLine1:

; Converte line1 (first Line is numeric)
push dword
push linha
call conv2num
add esp, 8
mov , eax
assignLine2:
; Converte line 2 (second Line is numeric)
push dword
push linha
call conv2num
add esp, 8
mov , eax
assignLine3:
mov ebx, currentLine
mov al, byte
cmp al, '+'
je Sum
cmp al, '-'
je Subtraction
cmp al, '*'
je Multiplication
cmp al, '/'
je Division

      .
      .
      .


Is working to a certain part ...
First jumps to proccessline is ok, I return to loop 1 and then gives the following error Segmentation fault
Posted on 2008-02-10 06:14:40 by nmpsantos

mov eax, dinamicString
mov ,eax

Umm, you store the offset of dinamicString in stringlenght? What is that supposed to achieve? You need to call a strlen() function when working with ASCIZ strings.


mov esi, dinamicString
mov edi, destinationString
mov ecx, stringlenght

This code is OK, if you fix the string length thing. Oh, and you might possibly need and , if those are dynamically allocated (malloc() / whatever), and not static-sized strings in .data or .bss .


ciclo:
lodsb
stosb
loopnz ciclo

replace this with rep movsb.

PS: "length" and "dynamic", not "lenght" and "dinamic" :)
Posted on 2008-02-10 06:53:49 by f0dder

I know that my questions may seem a stupid and lazy ...
But I'm a student worker and had only one 5 classes of NASM ...

Now I have to read a file,
get 2 operands,
get the operator,
and clear the file I read and write the result in the same file.

I am completely disoriented and desperate!

Thanks for your help
Posted on 2008-02-10 07:18:26 by nmpsantos

mov esi, data
mov edi, currentLine
mov edx, 0x000000FF
loop1:
mov ebx, 0x0000000A
mov eax, DWORD
and eax, edx
inc esi
cmp eax, ebx ;check character 0xA
je proccessline

lodsb
cmp al, '#'                            ;check end of the file ( I put this Character in the last line of my file)
je fileend
               
mov DWORD , eax
inc edi
jmp loop1

I don't understand!
This has worked but now I do not know why it is always the return Segmentation fault ...

He does not seem to be doing the inc's...
Attachments:
Posted on 2008-02-10 08:37:36 by nmpsantos