在MASM中反转字符串。获得奇怪的输出

时间:2018-10-26 18:51:35

标签: assembly output reverse masm irvine32

我试图在我的汇编语言类中反转一个字符串。我相信我有正确的代码,但输出仅为“ a”。这是代码:

var arr = [
{
    "itemType": "SelectionTitle",
    "name": "1105F.MID",
    "active": true,
    "isFactoryDefault": false,
    "factoryCode": "",
    "seasons": [],
    "denominations": [],
    "groups": [],
    "length": 0,
    "_id": "5ada2217c114ca048e1db9b0",
    "created_by": "5ab57289d8d00507b29a3fdd",
    "selectionFile": {
        "itemType": "SelectionFile",
        "name": "1105F.MID"
    }
},
{
    "itemType": "SelectionTitle",
    "name": "test",
    "active": true,
    "isFactoryDefault": false,
    "factoryCode": "",
    "seasons": [],
    "denominations": [],
    "groups": [],
    "length": 0,
    "_id": "5ada2217c114ca048e1db9b0",
    "created_by": "5ab57289d8d00507b29a3fdd",
    "selectionFile": {
        "itemType": "SelectionFile",
        "name": "testing"
    }
}
]

// this method will take care of adding new property
function replace(obj, from, to) {
	Object.entries(obj).forEach(([key, value]) => (
	    key == from && (obj[to] = obj[from])
	    , typeof value === "object" && replace(value, from, to)
	))
}

arr.forEach(d => replace(d, 'name', 'label'))

// you can check this log for property 'label' whereever 'name' exists
console.log(arr)

我不确定发生了什么。

1 个答案:

答案 0 :(得分:0)

字符串的长度需要考虑零索引,因此;

mov   esi, SIZEOF myString - 1         ; Should equal 8
mov   ecx, SIZEOF myString             ; Should equal 9

需要将终止字符0x0 NULL 写入 temp 的末尾。为了至少优化一点,写4个NULL的工作是因为EDI只是一个32位寄存器。

xor   edi, edi
mov   temp[ecx],edi                    ; Writing 4 chars

; Instead of

mov   BYTE temp[ecx], 0

循环中唯一需要更改的是;

@@:
mov   al, myString [edi]               ; Read char from source
mov   temp[esi], al                    ; and write to destination
dec   esi
inc   edi
loop  @B

您的程序正在发生的事情是

movzx eax, myString
mov   temp, al

将源的第一个字符移动到目标的第一个位置,并且由于您未能考虑零索引,因此源中的终止字符是写入 temp的下一个对象。