title: How to get the beginning address of a string instead of the address of a whole string
body: <p>SORRY if i make you confuse about the question, but i don't know a better to describe it</p>
  
  <p>i'm trying to write a NASM program to reverse a string in place.</p>
  
  <p>i have 3 function: strlen, readstring, and asm_main</p>
  
  <p>readstring: read from user input and store it in reserved space "inputstring"</p>
  
  <pre><code>    push ebp        ; setup routine
      mov ebp, esp
      ;;;;;;;;;;;;;;;;;;;;;;;;;; 
      mov eax, message
      call print_string   ; print"please enter your string message"
  
      mov eax, 3  ; read
      mov ebx, 1  ; from standard input
      mov ecx,inputstring;  store at memory locaton inputstring
      mov edx, 100        ; 100 byte
      int 0x80
  
      mov eax, inputstring
  
     ;;;;;;;;;;;;;;;;;;;;;
      pop ebp
      ret
  </code></pre>
  
  <p>Later, i use another subprogram "strlen" to get the length of users' input</p>
  
  <p><strong>The problem is</strong>:</p>
  
  <p>In my main program, i have two register hold the address of the beginning and the last char.  so ecx holds begin, ebx holds last char</p>
  
  <p>let's say i type "abcde", </p>
  
  <p>so i have $ecx point to the beginning of the string, and $ebx point to the last char(which is e\n). My question is, when i swaping ecx and ebx, am i swapping the whole string(abcde) with the last char(e/n)?  </p>
  
  <p>or am i swaping the first char(a) with the last char(e/n)?</p>
  
  <p>when i try to print out the content of each register in GDB.</p>
  
  <p><strong>ecx: abede/n</strong></p>
  
  <p><strong>ebx: e/n</strong></p>
  
  <p>so i'm swaping the whole string with the last char, which is definitely not i want. So where did i do wrong??? can someone pls give me a hint?</p>
  
  <p>Here is my code</p>
  
  <pre><code>%include "asm_io.inc"
  segment .data
  message db "Please enter your string message: ", 0xA,0
  formatin db "%d", 0xA
  
  
  segment .bss
     inputstring: resb 100
  
  segment .text
      global asm_main
      extern printf
      extern scanf
  
  
  **asm_main**:
      enter 0, 0
      pusha
  
      ;;;;;;;;;;;;;;;;;;;;;;;;
      call readstring
      mov ecx, eax    ; now ecx contains the begin address
      call strlen     ; when return, eax will hold the length
  
      push eax        ; we need eax in the loop
  
      ; use edi as temporary pointer
      ; use ebx to point to the last char
  
      mov ebx, 0    ;initialize ebx to 0
      add ebx, ecx  ; ebx = ecx now, ebx points to the beginning
      add ebx, eax  ; ebx = ecx + eax, now it points to then end
      sub ebx, 1    ; ebx = ecx +eax -1, now it points to the 
                    ; last char
  
  while:
      cmp ebx, ecx   ; cmp end , begining
      jle exitWhile
  
      mov edi, [ebx]    ; temp = *end
      mov eax,[ecx]   ; *end = *begining
      mov [ebx], eax
      mov [ecx], edi    ; *beginning = temp
  
      inc ecx
      dec ebx
  
      jmp while
  
  exitWhile:
  
      pop eax
      popa
      mov eax, 0
      leave           ; missing this line will cause segmentation 
                      ; fault, why?
      ret
  
  **readstring**:
      push ebp        ; setup routine
      mov ebp, esp
      ;;;;;;;;;;;;;;;;;;;;;;;;;; 
      mov eax, message
      call print_string
  
      mov eax, 3  ; read
      mov ebx, 1  ; from standard input
      mov ecx,inputstring;  store at memory locaton inputstring
      mov edx, 100        ; 100 byte
      int 0x80
  
      mov eax, inputstring
  
     ;;;;;;;;;;;;;;;;;;;;;
      pop ebp
      ret
  
  
  **strlen**:
      push ebp
      mov ebp, esp
  
      push esi
      ;;;;;;;;;;;;;;;;;;;;;
      mov esi, ecx    ; now esi contains the address of string
      mov edx, 0     ;  edx will be int i
  loop:
      mov al, [esi]   ; al 
      cmp al, 0xA     ; check if al holds an ASCII new line
      je  exitLoop
  
      inc esi
      inc edx
  
      cmp esi, inputstring+100; see if esi is pointing past the end
                      ; of the 100 reserved byte
  
      jl loop
  
  
  exitLoop:
      ;;;;;;;;;;;;;;;;;;;;
      mov eax, edx
      pop esi
      pop ebp
      ret
  </code></pre>