将LinkedList与String Reversal结合起来作为目标是理解LinkedList

时间:2017-05-08 01:20:52

标签: arrays assembly struct linked-list x86

我正在尝试使用链表程序,该程序将显示用户输入的反向字符串。以下是我的用于反转用户输入的字符串的程序。它还给出了字符串的长度:

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书中的这段代码,并将其与我的代码结合起来,以便我可以使用Linked List来显示反向字符串:< / p>

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

有人可以引导我走正确的道路。谢谢。

1 个答案:

答案 0 :(得分:2)

需要链接列表来反转字符串。不,真的。

但是,如果你确实坚持......

#include <vector>
#include <memory>

using namespace std;

class Base
{
public:
    int getVal() { return 0; }
};

int getVal(shared_ptr<Base>& p)
{
    return p->getVal();
}

int getVal(unique_ptr<Base>& p)
{
    return p->getVal();
}

int getVal(int*& p)
{
    return *p;
}


template <class Type>
bool writeRecordForSet(std::vector<Type>& entityPtr)
{
    if (entityPtr.size() == 0)
        return true;
    //...
    for (auto iter = entityPtr.begin(); iter != entityPtr.end(); iter++) {
        int myval = getVal(*iter);
    }

    return true;
}

int main()
{
    std::vector<std::shared_ptr<Base>> vec_shared;
    std::vector<int*> vec_intp;
    std::vector<std::unique_ptr<Base>> vec_unique_ptr;
    writeRecordForSet(vec_shared);
    writeRecordForSet(vec_intp);
    writeRecordForSet(vec_unique_ptr);
}

像这样。

它应该有用,但我还没有真正测试过。