From 12970c2da6ad0e29219be3ef198739816157696b Mon Sep 17 00:00:00 2001 From: Exzap <13877693+Exzap@users.noreply.github.com> Date: Wed, 6 May 2026 04:24:48 +0200 Subject: [PATCH] PPCAsm: Fix string parsing when string contains escaped double quote Also fix write position not being advanced after a string --- src/Cemu/PPCAssembler/ppcAssembler.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Cemu/PPCAssembler/ppcAssembler.cpp b/src/Cemu/PPCAssembler/ppcAssembler.cpp index 7b89a068..ab1c20c9 100644 --- a/src/Cemu/PPCAssembler/ppcAssembler.cpp +++ b/src/Cemu/PPCAssembler/ppcAssembler.cpp @@ -2360,6 +2360,7 @@ bool _ppcAssembler_emitDataDirective(PPCAssemblerContext& internalInfo, ASM_DATA // write string bytes + null-termination character internalInfo.ctx->outputData.insert(internalInfo.ctx->outputData.end(), stringData.data(), stringData.data() + stringData.size()); internalInfo.ctx->outputData.emplace_back(0); + writeIndex = internalInfo.ctx->outputData.size(); continue; } // numeric constants @@ -2492,6 +2493,16 @@ bool ppcAssembler_assembleSingleInstruction(char const* text, PPCAssemblerInOut* internalInfo.listOperandStr.clear(); bool isInString = false; + auto isEscaped = [](const char* operandStartPtr, const char* ptr) + { + size_t backslashCount = 0; + while (ptr > operandStartPtr && ptr[-1] == '\\') + { + backslashCount++; + ptr--; + } + return (backslashCount & 1) != 0; + }; while (currentPtr < endPtr) { @@ -2500,7 +2511,7 @@ bool ppcAssembler_assembleSingleInstruction(char const* text, PPCAssemblerInOut* // find end of operand while (currentPtr < endPtr) { - if (*currentPtr == '"') + if (*currentPtr == '"' && !isEscaped(startPtr, currentPtr)) isInString=!isInString; if (*currentPtr == ',' && !isInString) @@ -3587,6 +3598,9 @@ void ppcAsmTestDisassembler() _testAsmArray({ 0x7f }, ".byte 0x7f"); _testAsmArray({ 0x74, 0x65, 0x73, 0x74, 0x00 }, ".byte \"test\""); _testAsmArray({ 0x41, 0x42, 0x43, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00 }, ".byte \"ABC\", \"test\""); + _testAsmArray({ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x22, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x00, 0x00 }, R"(.byte "hello\"world", 0)"); + _testAsmArray({ 0x61, 0x5c, 0x00, 0x00 }, R"(.byte "a\\", 0)"); + _testAsmArray({ 0x61, 0x5c, 0x22, 0x62, 0x00, 0x00 }, R"(.byte "a\\\"b", 0)"); }