Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we stop replacing subtracted values with added negative versions? #6683

Open
Wall-AF opened this issue Jul 1, 2024 · 0 comments
Open

Comments

@Wall-AF
Copy link

Wall-AF commented Jul 1, 2024

Is your feature request related to a problem? Please describe.
When Ghidra produces a C-like expressions involving the x86 assembly instruction SUB, the subracted value is replaced with a + -val instead of just - val as in the following example:
*(char *)address = cbuffer[0] + *address * 10 + -0x30;
This means you cannot simply convert the 0x30 into its character representation as the -'ve is taken into account producing '\xd0' instead of '0'.

Describe the solution you'd like
An option to keep subtracted values positive and change the sign in the resulting expression. Maybe even on a case by case basis.

Describe alternatives you've considered
Can't think of any presently!

Additional context
Below is the pcode for the expression above:

    1000913d b1 0a        090           MOV        CL,0xa
                                                      CL = COPY 10:1
    1000913f 8a 07        090           MOV        AL,byte ptr [EDI]
                                                      $U9100:1 = LOAD ram(EDI)
                                                      AL = COPY $U9100:1
    10009141 f6 e9        090           IMUL       CL
                                                      $U26780:2 = INT_SEXT AL
                                                      $U26800:2 = INT_SEXT CL
                                                      AX = INT_MULT $U26780:2, $U26800:2
                                                      $U10900:2 = INT_SEXT AL
                                                      CF = INT_NOTEQUAL $U10900:2, AX
                                                      OF = COPY CF
    10009143 8a 4c 24 10  090           MOV        CL,byte ptr [ESP + cbuffer[0]]
                                                      $U3780:4 = INT_ADD 16:4, ESP
                                                      $U9100:1 = LOAD ram($U3780:4)
                                                      CL = COPY $U9100:1
    10009147 83 c6 02     090           ADD        ESI,0x2
                                                      CF = INT_CARRY ESI, 2:4
                                                      OF = INT_SCARRY ESI, 2:4
                                                      ESI = INT_ADD ESI, 2:4
                                                      SF = INT_SLESS ESI, 0:4
                                                      ZF = INT_EQUAL ESI, 0:4
                                                      $Uf980:4 = INT_AND ESI, 0xff:4
                                                      $Ufa00:1 = POPCOUNT $Uf980:4
                                                      $Ufa80:1 = INT_AND $Ufa00:1, 1:1
                                                      PF = INT_EQUAL $Ufa80:1, 0:1
    1000914a 02 c8        090           ADD        CL,AL
                                                      CF = INT_CARRY CL, AL
                                                      OF = INT_SCARRY CL, AL
                                                      CL = INT_ADD CL, AL
                                                      SF = INT_SLESS CL, 0:1
                                                      ZF = INT_EQUAL CL, 0:1
                                                      $Uf980:1 = INT_AND CL, 0xff:1
                                                      $Ufa00:1 = POPCOUNT $Uf980:1
                                                      $Ufa80:1 = INT_AND $Ufa00:1, 1:1
                                                      PF = INT_EQUAL $Ufa80:1, 0:1
    1000914c 80 e9 30     090           SUB        CL,0x30
                                                      CF = INT_LESS CL, 48:1
                                                      OF = INT_SBORROW CL, 48:1
                                                      CL = INT_SUB CL, 48:1
                                                      SF = INT_SLESS CL, 0:1
                                                      ZF = INT_EQUAL CL, 0:1
                                                      $Uf980:1 = INT_AND CL, 0xff:1
                                                      $Ufa00:1 = POPCOUNT $Uf980:1
                                                      $Ufa80:1 = INT_AND $Ufa00:1, 1:1
                                                      PF = INT_EQUAL $Ufa80:1, 0:1
    1000914f 81 fe d0     090           CMP        ESI,DAT_1000d6d0
             d6 00 10
                                                      $U1da00:4 = COPY ESI
                                                      CF = INT_LESS $U1da00:4, 0x1000d6d0:4
                                                      OF = INT_SBORROW $U1da00:4, 0x1000d6d0:4
                                                      $U1db00:4 = INT_SUB $U1da00:4, 0x1000d6d0:4
                                                      SF = INT_SLESS $U1db00:4, 0:4
                                                      ZF = INT_EQUAL $U1db00:4, 0:4
                                                      $Uf980:4 = INT_AND $U1db00:4, 0xff:4
                                                      $Ufa00:1 = POPCOUNT $Uf980:4
                                                      $Ufa80:1 = INT_AND $Ufa00:1, 1:1
                                                      PF = INT_EQUAL $Ufa80:1, 0:1
    10009155 88 0f        090           MOV        byte ptr [EDI],CL
                                                      $U4e00:1 = COPY CL
                                                      STORE ram(EDI), $U4e00:1

Interestingly, only the assembly at the addresses below is highlighted (indicating their involvement) as the C expression's constituent parts!
1000913f
10009141
1000914a
1000914c
10009155

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant