Type in inline assembly - inline-assembly

In some code I was writing, I had the following line:
__asm mov [ebp-24], 0
...from which the compiler generated
mov BYTE PTR [ebp-24], 0
Is there any way I can make it generate
mov DWORD PTR [ebp-24], 0
instead?

Depending on your assembler (you didn't state what you are using), try:
mov dword ptr [ebp-24], 0
or
movl [ebp-24], 0

Related

How to inform the optimizer that NonZeroU32::get will never return zero?

Here's the code sample where I ran into the problem:
pub fn div(x: u32, y: u32) -> u32 {
x / y
}
pub fn safe_div(x: u32, y: std::num::NonZeroU32) -> u32 {
x / y.get() // an unchecked division expected
}
Godbolt's rustc 1.47.0 -O generates the same assembly for both functions:
example::div:
push rax
test esi, esi
je .LBB0_2
mov eax, edi
xor edx, edx
div esi
pop rcx
ret
.LBB0_2:
lea rdi, [rip + str.0]
lea rdx, [rip + .L__unnamed_1]
mov esi, 25
call qword ptr [rip + core::panicking::panic#GOTPCREL]
ud2
example::safe_div:
push rax
test esi, esi
je .LBB1_2
mov eax, edi
xor edx, edx
div esi
pop rcx
ret
.LBB1_2:
lea rdi, [rip + str.0]
lea rdx, [rip + .L__unnamed_2]
mov esi, 25
call qword ptr [rip + core::panicking::panic#GOTPCREL]
ud2
However, it is statically known that
checking NonZeroU32::get's result against zero is pointless.
Can I somehow make the optimizer believe it
(maybe with creating new structs for this) in an unsafeless way?
Related GitHub issue #49572
After I saw your question, I added the needed impls to std,
so now the nightly release (and the upcoming 1.51 stable release) supports this!
Godbolt for
pub fn div(x: u32, y: u32) -> u32 {
x / y
}
pub fn safe_div(x: u32, y: std::num::NonZeroU32) -> u32 {
x / y
}
pub fn safe_rem(x: u32, y: std::num::NonZeroU32) -> u32 {
x % y
}
Produces the expected assembly:
example::div:
push rax
test esi, esi
je .LBB0_2
mov eax, edi
xor edx, edx
div esi
pop rcx
ret
.LBB0_2:
lea rdi, [rip + str.0]
lea rdx, [rip + .L__unnamed_1]
mov esi, 25
call qword ptr [rip + core::panicking::panic#GOTPCREL]
ud2
example::safe_div:
mov eax, edi
xor edx, edx
div esi
ret
example::safe_rem:
mov eax, edi
xor edx, edx
div esi
mov eax, edx
ret

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

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.

MASM division for negative number

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