Подтвердить что ты не робот

Объединение LinkedList со String Reversal as Goal заключается в понимании LinkedList

Я пытаюсь использовать связанную программу списка, которая отображает обратную строку, введенную пользователем. Следующей является моя программа для изменения строки, введенной пользователем. Он также дает длину строки:

TITLE ReadStringProc (ReadStringProc.asm)

include irvine32.inc

ListNode STRUCT 
    NodeData DWORD ?
    NextPtr DWORD ? 
ListNode ENDS 

NULL = 0
Counter = 0

.data
input               byte        100 dup(0)                         
stringinput         byte        "Enter any string: ",0         
totallength         byte        "The total length is: ",0   
reverse             byte        "The reverse string is: ",0

LinkedList LABEL DWORD
REPT input
    Counter = Counter + 1
    ListNode <Counter, ($ + Counter * SIZEOF ListNode)>
ENDM
ListNode <0,0>  ; tail node




.code 




stringLength        proc
                    push        ebp
                    mov         ebp, esp
                    push        ebx
                    push        ecx
                    mov         eax, 0
                    mov         ebx, [ebp+8]
L1:
                    mov         ecx, [ebx] ;you can use ecx, cx, ch, cl 
                    cmp         ecx, 0     ;you can use ecx, cx, ch, cl 
                    JE          L2
                    add         ebx, 1
                    add         eax, 1
                    jmp         L1
L2:
                    pop         ecx
                    pop         ebx
                    mov         ebp, esp
                    pop         ebp
                    ret         4

stringLength        endp

swap MACRO          first,last
                    push        eax 
                    mov         ah, first
                    mov         al, last
                    xor         al, ah ;x
                    xor         ah, al ;y 
                    xor         al, ah ;x
                    mov         last, al
                    mov         first, ah
                    pop         eax
endM

stringReverse proc 
                    push        ebp
                    mov         ebp, esp
                    push        OFFSET input  
                    call        stringLength
                    mov         edx, [ebp+8]   ;edx = offset string to reverse 
                    mov         esi, offset 0
                    dec         eax    
                    mov         ebx,edx       ;ebx stores the pointer to the first character  
                    add         ebx,eax       ;now ebx store the pointer to the last character before the '$'  
                    reverseloop:   
                    push        edx 
                    push        ebx 
                    swap        [edx], [ebx]
                    inc         edx          ;increment  of the right-most pointer
                    dec         ebx          ;decrement of the right-most pointer 
                    cmp         edx, ebx     ;compares the left-most pointer to the right-most 
                    jb          reverseloop
jmp stopEnd      ;"ja", there is no need to check a condition twice 

stopEnd:
                    mov         esp, ebp
                    pop         ebp
ret 4

stringReverse       endp

main proc

                    call        clrscr              
                    mov         edx, offset stringinput     
                    call        writeString
                    mov         edx, offset input 
                    call        writeString      
                    call        stringLength
                    mov         edx, offset input 
                    mov         ecx, sizeof input 
                    call        readstring        
                    call        crlf
                    mov         edx,offset totallength
                    call        writestring
                    call        writedec    
                    call        crlf
                    mov         edx, offset reverse
                    call        crlf
                    call        writeString
                    push        offset input
                    call        stringReverse
                    mov         edx, offset input 
                    call        writeString     
                    call        crlf

                    exit 
main                endp


                    end         main

Какова моя цель - найти способ использовать этот фрагмент кода, который содержится в книге Kip Irvine Assembly x86, и объединить его с кодом, который у меня есть, чтобы я мог использовать Связанный список для отображения обратной строки:

ListNode STRUCT
  NodeData DWORD ?
  NextPtr  DWORD ?
ListNode ENDS

TotalNodeCount = 15
NULL = 0
Counter = 0

.data
LinkedList LABEL DWORD
REPT TotalNodeCount
    Counter = Counter + 1
    ListNode <Counter, ($ + Counter * SIZEOF ListNode)>
ENDM
ListNode <0,0>  ; tail node

.code
main PROC
    mov  esi,OFFSET LinkedList

; Display the integers in the NodeData members.
NextNode:
    ; Check for the tail node.
    mov  eax,(ListNode PTR [esi]).NextPtr
    cmp  eax,NULL
    je   quit

    ; Display the node data.
    mov  eax,(ListNode PTR [esi]).NodeData
    call WriteDec
    call Crlf

    ; Get pointer to next node.
    mov  esi,(ListNode PTR [esi]).NextPtr
    jmp  NextNode

quit:
    exit

Может ли кто-нибудь привести меня к правильному пути. Спасибо.

4b9b3361

Ответ 1

Вам не нужны связанные списки для изменения строки. Нет, серьезно.

Однако, если вы настаиваете...

stringReverse proc;     <-- Warning: EBX and ESI are not preserved!
  push ebp;
  mov ebp, esp;
  mov ebx, [ebp + 8];   <-- EBX = target string pointer
  push ebx;
  call stringLength;    <-- we trust that stringLength() preserves EBX
;  mov ebx, [ebp + 8];   <-- uncomment this if it actually does not
  mov esi, ebx;
  xor edx, edx;
  add ebx, eax;
  neg eax;

  @fill:
    mov cl, [ebx + eax];
    sub esp, 8;         <-- allocating a new linked list item on stack
    mov [esp], cl;      <-- 1st DWORD / 1st BYTE = current character
    mov [esp + 4], edx; <-- 2nd DWORD = next item pointer
    mov edx, esp;       <-- EDX = current item pointer
    inc eax;
  jne @fill;

  @roll:
    mov cl, [edx];
    mov [esi], cl;
    inc esi;
    mov edx, [edx + 4]; <-- next item, here we go!
    test edx, edx;      <-- what if we are done?
  jne @roll;

  mov esp, ebp;         <-- discarding the allocated stack
  pop ebp;
  ret 4;

Что-то вроде этого.

Он должен работать, но я действительно не тестировал.