0

I need need to store the two’s complement of var1, var2, var3, and var4 in register ebx. The two’s complement of var1 is in the highest byte of ebx, the two’s complement of var2 is the next byte, the two’s complement of var3 is in the next byte, and the two’s complement of var4 is in the lowest byte. Remember that for a hexadecimal number, there are two steps to get the two’s complement:

  1. subtract the number from 15
  2. Add 1

For example, the ‘D’ (44h) is stored in the highest byte of eax, so the two’s complement of 44h will be stored in the highest byte of ebx. The two’s complement of 44h is:

  1. 15 – 4 = B (11) and 15 – 4 = B (11) So we get BBh after step 1

  2. BB + 1 = BC So the two’s complement of 44h is BCh, and this is what should end up in the highest byte of ebx

HINT: Think about how the sub instruction could be used to get the two’s complement of a number. NOTE: You should use the same shifting algorithm that you used for phase 2 here. Start by placing the two’s complement of var1 and var2 into the lower 16 bits of ebx, and then shift them into the upper 16 bits of ebx. Once that is completed, directly move the two’s complement of var3 and var4 to the lower 16 bits of ebx. After phase 3 is completed, register ebx should look like this: BCBFBEBD

Here is the code I have so far, which was shifting around the values of the variables and story the values into EAX

.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword

; defining variables 
.data
    var1 BYTE 'A'
    var2 BYTE 'B'
    var3 BYTE 'C'
    var4 BYTE 'D'
    
.code
main proc
    
    mov ah, var1
    mov al, var4
    mov bh, var2
    mov bl, var3
    mov var1, al
    mov var2, ah
    mov var3, bh
    mov var4, bl
    
    ;move var1 into ah
    ;move var2 into al
    ;add eax to eax 16 times to rotate var1 and var2 into the highest bytes
    mov ah, var1
    mov al, var2
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax
    add eax, eax

    ;move var3 and var4 into the lowest byte postions 
    mov ah, var3
    mov al, var4

    invoke ExitProcess,0
main endp
end main
  • i was only allowed to use add, mov and sub instructions – Robert Furger May 02 '21 at 16:01
  • 1
    Related: [How to mov values in eax register, ah and al left by 2 bytes? x86 Assembly](https://stackoverflow.com/q/66630555) for ways of shifting, but that doesn't mention the 2's complement part. I know I've seen a previous question recently that had the same weird "subtract from 15" / "add 1" explanation, i.e. subtract from 16. Which only makes sense for 4-bit numbers. But google isn't turning it up, nor is SO search. Maybe it got deleted. – Peter Cordes May 02 '21 at 16:06
  • 2
    The normal way to do 2's complement negation is just to subtract from zero. e.g. `sub ebx,ebx` (EBX=0) / `sub bl, var4` / `sub bh, var3` – Peter Cordes May 02 '21 at 16:09

0 Answers0