MASM division for negative number - division

I'm trying divide two numbers in assembly[Irvine-Intel x86 processor].
Here is my code:
mov eax, 4
mov edx, 0
mov ebx, 2
div ebx
I get the correct answer for the division, but when i change the value to a negative number, it does not give me the correct answer.
mov eax, -4
mov edx, 0
mov ebx, 2
div ebx
As I know, the "div" is unsigned divide. So how if I want to do division with negative number?

You should use IDIV.
IDIV instruction is a divide operation. IDIV is for signed numbers wheareas DIV is for unsigned numbers.
mov eax, -4
xor edx, edx ; mov edx, 0
mov ebx, 2
div ebx

Related

Accessing/Adding to a 2D C++ array using inline assembly

I'm trying to create an encryption/decryption program using c++ and assembly. I was originally using 3 different arrays, the original string, the encrypted string, and the decrypted string. I'm now trying to convert this to a 2D array and I'm having trouble adding an encrypted character to the second line of the array.
__asm
{
push eax
push ebx
push ecx
push edx
xor ebx, ebx
jmp checkend
forloop1: add ebx, 1
checkend: cmp ebx, aLength
jge endfor1
movzx ecx, byte ptr[Chars + ebx]
lea eax, EKey
push ecx
mov edi, eax
movzx eax, byte ptr[eax]
and eax, 0x3f
rol al, 1
mov byte ptr[edi], al
cmp eax, 0x00
jnz zerocheck
mov eax, 0x09
zerocheck:mov edx, eax
pop eax
sub eax, edx
rol al, 1
not al
mov byte ptr [Chars + ebx * 2], al // this line here
jmp forloop1
endfor1:
pop edx
pop ecx
pop ebx
pop eax
}
return;
I can read in the first row fine, which is where the string entered is stored. I've marked the line where I'm trying to get the encrypted character to be stored in the second line the array but everything I've tried doesn't seem to work. The [Chars + ebx * 2] is what isn't working. I know that the 2 on the end isn't the correct value, can anyone tell me what value I should be adding/multiplying on the end to get to the second line of the array. Any help would be appreciated.
Managed to get this working. All I had to do was add this line of code first;
mov edx, aLength
Where aLength is the length of the array, and then use that to get the second line in the array like so;
mov byte ptr [Chars + ebx + edx], al

Floating operations. Is there loop-carried dependency?

The following code presents QuickSort. I am trying to make it faster. Please help.
global sort
sort:
push ebp
mov ebp, esp
push edi
push esi
push ebx
sub esp, 28
mov eax, dword [ebp+8]
mov esi, dword [ebp+12]
mov dword [ebp-32], 1
lea edi, [eax-1]
sal eax, 2
lea edx, [eax+18]
movss xmm1, dword [esi-4+eax]
mov dword [ebp-28], edi
and edx, -16
sub esp, edx
lea edx, [esp+3]
shr edx, 2
lea ebx, [0+edx*4]
mov dword [4+edx*4], edi
mov dword [0+edx*4], 0
xor edi, edi
mov dword [ebp-36], ebx
.L14:
mov eax, dword [ebp-28]
lea edx, [edi-1]
cmp eax, edi
lea ebx, [esi+eax*4]
jle .L15
lea eax, [esi+edi*4]
.L18:
movss xmm0, dword [eax]
comiss xmm1, xmm0
jb .L16
add edx, 1
lea ecx, [esi+edx*4]
movss xmm2, dword [ecx]
movss dword [ecx], xmm0
movss dword [eax], xmm2
.L16:
add eax, 4
cmp eax, ebx
jne .L18
movss xmm1, dword [ebx]
.L15:
lea eax, [esi+4+edx*4]
cmp edx, edi
movss xmm0, dword [eax]
movss dword [eax], xmm1
movss dword [ebx], xmm0
jg .L19
sub dword [ebp-32], 2
lea edi, [edx+2]
cmp edi, dword [ebp-28]
jl .L25
.L21:
mov eax, dword [ebp-32]
test eax, eax
js .L11
mov edi, dword [ebp-36]
lea eax, [edi+eax*4]
mov edi, dword [eax]
mov ecx, edi
mov dword [ebp-28], edi
mov edi, dword [eax-4]
movss xmm1, dword [esi+ecx*4]
jmp .L14
.L19:
mov edi, dword [ebp-32]
mov eax, dword [ebp-36]
mov dword [eax+edi*4], edx
lea edi, [edx+2]
cmp edi, dword [ebp-28]
jge .L21
.L25:
mov ebx, dword [ebp-32]
mov ecx, dword [ebp-36]
movaps xmm1, xmm0
lea eax, [ecx+ebx*4]
mov ecx, dword [ebp-28]
mov dword [eax+4], edi
mov dword [eax+8], ecx
mov eax, ebx
add eax, 2
mov dword [ebp-32], eax
jns .L14
.L11:
lea esp, [ebp-12]
pop ebx
pop esi
pop edi
pop ebp
ret

Getting access-violation in QueryInterface API

I am getting a crash in MMC code. The dump is pointing to p->QueryInterface(GetIID(), reinterpret_cast<void**>(&pInterface));
The disassembly shows as below,
//fill method is called.
BSecRem!std::fill<int *,int>:
5d931510 8b442404 mov eax,[esp+0x4]
5d931514 8b4c2408 mov ecx,[esp+0x8]
5d931518 3bc1 cmp eax,ecx
5d93151a 7411 jz BSecRem!std::fill<int *,int>+0x1d (5d93152d)
5d93151c 8b54240c mov edx,[esp+0xc]
5d931520 56 push esi
5d931521 8b32 mov esi,[edx]
5d931523 8930 mov [eax],esi
5d931525 83c004 add eax,0x4
5d931528 3bc1 cmp eax,ecx
5d93152a 75f5 jnz BSecRem!std::fill<int *,int>+0x11 (5d931521)
5d93152c 5e pop esi
5d93152d c3 ret
5d93152e cc int 3
5d93152f cc int 3
BSecRem!stdext::_Unchecked_move_backward<int *,int *>:
5d931530 8b442408 mov eax,[esp+0x8]
5d931534 8b542404 mov edx,[esp+0x4]
5d931538 2bc2 sub eax,edx
5d93153a c1f802 sar eax,0x2
5d93153d 56 push esi
5d93153e 8b742410 mov esi,[esp+0x10]
5d931542 8d0c8500000000 lea ecx,[00000000+eax*4]
5d931549 2bf1 sub esi,ecx
5d93154b 85c0 test eax,eax
5d93154d 7e0d jle BSecRem!stdext::_Unchecked_move_backward<int *,int *>+0x2c (5d93155c)
5d93154f 51 push ecx
5d931550 52 push edx
5d931551 51 push ecx
5d931552 56 push esi
5d931553 ff15e034a65d call dword ptr [BSecRem!_imp__memmove_s (5da634e0)]
5d931559 83c410 add esp,0x10
5d93155c 8bc6 mov eax,esi
5d93155e 5e pop esi
5d93155f c3 ret
BSecRem!_com_ptr_t<_com_IIID<PATENTTEMPOCXLib::_DPatentTempOCX,&_GUID_6042985e_c7cc_4286_b5c4_48a347173739> >::_QueryInterface<IUnknown *>:
5d931560 8b442404 mov eax,[esp+0x4]
5d931564 56 push esi
5d931565 8bf1 mov esi,ecx
5d931567 85c0 test eax,eax
5d931569 7439 jz instant.dll!_com_ptr_t<_com_IIID<PATENTTEMPOCXLib::_DPatentTempOCX,&_GUID_7072787e_ddeb_1223_9aab4_9aab4d923e31> >::_QueryInterface<IUnknown *>+0x44 (5d9315a4)
5d93156b 8b08 mov ecx,[eax]
5d93156d 53 push ebx
5d93156e 57 push edi
5d93156f 8d542410 lea edx,[esp+0x10]
5d931573 52 push edx
5d931574 68cc54a65d push 0x5da654cc
5d931579 50 push eax
5d93157a 8b01 mov eax,[ecx] ds:002b:0000000c=???????
The crash is seen frequently. Specially when I navigate from one interface to other interface of MMC.
Thanks in advance.

Reading a two digit number in assembly and storing it in a variable

I need to a program in assembly to read a two digit number from the user, store it in a variable and later print it.
I have tried a lot, but could not get through.
Here is my coding.
.model small
.stack 100h
.data
msg db "Enter a number: $"
msg2 db "You have entered: $"
num1 db 0
num2 db 0
temp db 0
ten db 10
readNum db 0
t2 db 0
t1 db 0
.code
mov ax,#data
mov ds,ax
call read
call endL
call write
proc endL
mov dl,0ah
mov ah,02h
int 21h
ret
endp
proc read
mov dx,offset msg
mov ah,09h
int 21h
mov ah,01h
int 21h
mov num1,al
mul ten
mov temp,al
mov dl,temp
add dl,48
mov ah,02h
int 21h
mov ah,01h
int 21h
mov num2,al
mov dl,num2
add dl,temp
mov readNum,dl
ret
endp
proc write
mov dx,offset msg2
mov ah,09h
int 21h
mov al,readNum
mov ah,00
div ten
mov t1,ah
mov t2,al
mov dl,t1
add dl,48
mov ah,02h
int 21h
mov dl,t2
add dl,48
mov ah,02h
int 21h
endp
mov ax,4c00h
int 21h
end
According to the above program, if I enter 42, it gives me the entered number as 85.
I could not find the error in my program. Can somebody help me please.
Check out the following program. I have edited your one. There is a small mistake as already mentioned by Frank Kotler. That is you didn't convert the user input to digit. You have added 48 to the user input. But you have to subtract 48 from it.
.model small
.stack 100h
.data
msg db "Enter a number: $"
msg2 db "You have entered: $"
num1 db 0
num2 db 0
temp db 0
ten db 10
readNum db 0
t2 db 0
t1 db 0
.code
mov ax,#data
mov ds,ax
call read
call endL
call write
proc endL
mov dl,0ah
mov ah,02h
int 21h
ret
endp
proc read
mov dx,offset msg
mov ah,09h
int 21h
mov ah,01h
int 21h
sub al,48
mov num1,al
mov ah,01h
int 21h
sub al,48
mov num2,al
mov al,num1
mul ten
add al,num2
mov readNum,al
ret
endp
proc write
mov dx,offset msg2
mov ah,09h
int 21h
mov al,readNum
mov ah,00
div ten
mov dl,ah
mov t2,dl
mov dl,al
add dl,48
mov ah,02h
int 21h
mov dl,t2
add dl,48
mov ah,02h
int 21h
endp
mov ax,4c00h
int 21h
end
Apart from what others have pointed out about not converting ASCII value to number I noticed that you are using too many variables unnecessarily. If you just need to take one number from the user and display it, you just need space to store that one number.
You might want to check out this page as a reference.
Hope it'll solve your problem.

Objective C selector table functions

I am trying to figure out which method objc_msgSend() will call.
While looking at assembly of any function in Appkit or Cocoa or Any framework based on Objective-C framework, I always get stucked at objc_msgSend().
Does any one has any idea how to reach the actual function call from here?
I remember while browsing I have seen some open source code which was kind of Map for which function will be called based on selector (integer value). I guess it was from Apple (not sure). I am trying to find that link from almost 2 days, but cannot find it. If anyone who that link please share.
In following assembly, I am trying to solve this call
__text:001C6E46 call _objc_msgSend
It should be great, if some one can help me understand the complete procedure.
__text:001C6DC0 ; -[NSWindow _windowMovedToRect:]
__text:001C6DC0 __NSWindow__windowMovedToRect__ proc near ; DATA XREF: __inst_meth:00993418o
__text:001C6DC0
__text:001C6DC0 var_C = dword ptr -0Ch
__text:001C6DC0 arg_0 = dword ptr 8
__text:001C6DC0 arg_8 = dword ptr 10h
__text:001C6DC0 arg_C = dword ptr 14h
__text:001C6DC0 arg_10 = dword ptr 18h
__text:001C6DC0 arg_14 = dword ptr 1Ch
__text:001C6DC0
__text:001C6DC0 push ebp
__text:001C6DC1 mov ebp, esp
__text:001C6DC3 push esi
__text:001C6DC4 push ebx
__text:001C6DC5 sub esp, 30h
__text:001C6DC8 call $+5
__text:001C6DCD pop ebx
__text:001C6DCE mov esi, [ebp+arg_0]
__text:001C6DD1 lea eax, [ebp+var_C]
__text:001C6DD4 mov [esp], eax
__text:001C6DD7 call _PSbuttondown
__text:001C6DDC mov edx, [ebp+var_C]
__text:001C6DDF test edx, edx
__text:001C6DE1 jz short loc_1C6E1D
__text:001C6DE3 mov eax, [esi+80h]
__text:001C6DE9 or dword ptr [eax+0B8h], 1000000h
__text:001C6DF3 mov eax, [ebp+arg_8]
__text:001C6DF6 mov edx, [ebp+arg_C]
__text:001C6DF9 mov [esp+8], eax
__text:001C6DFD mov [esp+0Ch], edx
__text:001C6E01 mov eax, [esi+8]
__text:001C6E04 mov edx, [esi+0Ch]
__text:001C6E07 mov [esp], eax
__text:001C6E0A mov [esp+4], edx
__text:001C6E0E call _NSEqualPoints
__text:001C6E13 test al, al
__text:001C6E15 jnz loc_1C6ED6
__text:001C6E1B jmp short loc_1C6E50
__text:001C6E1D ; ---------------------------------------------------------------------------
__text:001C6E1D
__text:001C6E1D loc_1C6E1D: ; CODE XREF: -[NSWindow _windowMovedToRect:]+21j
__text:001C6E1D mov eax, [ebp+arg_8]
__text:001C6E20 mov [esp+8], eax
__text:001C6E24 mov eax, [ebp+arg_C]
__text:001C6E27 mov [esp+0Ch], eax
__text:001C6E2B mov eax, [ebp+arg_10]
__text:001C6E2E mov [esp+10h], eax
__text:001C6E32 mov eax, [ebp+arg_14]
__text:001C6E35 mov [esp+14h], eax
__text:001C6E39 mov eax, ds:(off_926DC8 - 1C6DCDh)[ebx]
__text:001C6E3F mov [esp+4], eax
__text:001C6E43 mov [esp], esi
__text:001C6E46 call _objc_msgSend
__text:001C6E4B jmp loc_1C6ED6
__text:001C6E50 ; ---------------------------------------------------------------------------
__text:001C6E50
__text:001C6E50 loc_1C6E50: ; CODE XREF: -[NSWindow _windowMovedToRect:]+5Bj
__text:001C6E50 mov eax, [ebp+arg_8]
__text:001C6E53 mov [esp+8], eax
__text:001C6E57 mov eax, [ebp+arg_C]
__text:001C6E5A mov [esp+0Ch], eax
__text:001C6E5E mov eax, [ebp+arg_10]
__text:001C6E61 mov [esp+10h], eax
__text:001C6E65 mov eax, [ebp+arg_14]
__text:001C6E68 mov [esp+14h], eax
__text:001C6E6C mov eax, ds:(off_91D198 - 1C6DCDh)[ebx]
__text:001C6E72 mov [esp+4], eax
__text:001C6E76 mov [esp], esi
__text:001C6E79 call _objc_msgSend
__text:001C6E7E mov eax, ds:(off_91A4B8 - 1C6DCDh)[ebx]
__text:001C6E84 mov [esp+4], eax
__text:001C6E88 mov eax, ds:(off_928EE8 - 1C6DCDh)[ebx]
__text:001C6E8E mov [esp], eax
__text:001C6E91 call _objc_msgSend
__text:001C6E96 mov [esp+0Ch], esi
__text:001C6E9A mov edx, ds:(_NSWindowDidMoveNotification_ptr - 1C6DCDh)[ebx]
__text:001C6EA0 mov edx, [edx]
__text:001C6EA2 mov [esp+8], edx
__text:001C6EA6 mov edx, ds:(off_91B0DC - 1C6DCDh)[ebx]
__text:001C6EAC mov [esp+4], edx
__text:001C6EB0 mov [esp], eax
__text:001C6EB3 call _objc_msgSend
__text:001C6EB8 mov eax, ds:(_NSAccessibilityMovedNotification_ptr - 1C6DCDh)[ebx]
__text:001C6EBE mov eax, [eax]
__text:001C6EC0 mov [esp+8], eax
__text:001C6EC4 mov eax, ds:(off_91B030 - 1C6DCDh)[ebx]
__text:001C6ECA mov [esp+4], eax
__text:001C6ECE mov [esp], esi
__text:001C6ED1 call _objc_msgSend
__text:001C6ED6
__text:001C6ED6 loc_1C6ED6: ; CODE XREF: -[NSWindow _windowMovedToRect:]+55j
__text:001C6ED6 ; -[NSWindow _windowMovedToRect:]+8Bj
__text:001C6ED6 add esp, 30h
__text:001C6ED9 pop ebx
__text:001C6EDA pop esi
__text:001C6EDB leave
__text:001C6EDC retn
__text:001C6EDC __NSWindow__windowMovedToRect__ endp
This site mentions a tool called otx that appears to decode the selector for you.