Asm multiplication

Started by Frank Brübach, May 21, 2024, 12:49:14 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach

Hello

Simple question I have how to multiply two values in asm?

' multiplication of two values
'
byte val1
byte val2
int result

macro myMul(val1,val2)
    val1=5
    val2=10

    ! mov eax,val1
    ''! mov eax,val2
    ! mul val2
    ! mov result,wax
end macro

'print "the result of val1*val2 is " + str(myMul(val1,val2)) 
'print "the result of val1*val2 is " + str(result) 

Theo Gottwald

#1
That is not a simple question, as you do not say what datatypes you want to multiply.
Then there may be differences in handling signed, unsigned Integer and/or FP-Data.
FP-Numbers can also be different formats.

Let me say that such questions get most easily answers if you just let AI explain it to you.



QuoteTo multiply two numbers in x86 assembly, you can use the mul instruction. Here is an example of how to multiply two numbers stored in the eax and ebx registers:

Move the first number into the eax register:
mov eax, first_number

Move the second number into the ebx register:
mov ebx, second_number

Multiply the two numbers using the mul instruction. The mul instruction multiplies the value in the eax register by the value in the specified register or memory location, and stores the result in the eax (lower 32 bits) and edx (upper 32 bits) registers:
mul ebx

The result of the multiplication is now stored in the eax register (if the result is less than 32 bits) or in the edx:eax register pair (if the result is 64 bits).
Here is the complete code:


section .data
    first_number dd 5
    second_number dd 10

section .bss
    result resd 1

section .text
    global _start

_start:
    ; load the first number into eax
    mov eax, [first_number]

    ; load the second number into ebx
    mov ebx, [second_number]

    ; multiply eax by ebx
    mul ebx

    ; store the result in [result]
    mov [result], eax

    ; exit the program
    mov eax, 1
    xor ebx, ebx
    int 0x80
In this example, the first number is 5 and the second number is 10. The result of the multiplication is 50, which is stored in the eax register.

Frank Brübach

Hi I have the solution

' multiplication of two values
'
byte val1
byte val2
int result ''resd 1
   
    val1=4
    val2=6 '

     mov eax,val1 
     mov eax,val2  ''multiply eax by ebx  "
     mul val1 'ebx 
     mov result,eax
     
print "the result of val1*val2 is " + str(result) '24

Frank Brübach

Hello...

New question How I  can add the result of multiplication to the call Message Box Line so result get shown in Message Box too?


' multiplication of two values
'
Declare Function MessageBox Lib "user32.dll" alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long

zstring tit[] = " PowerBASIC"
zstring msg[] = " hello my powerbasic friends"

byte val1
byte val2
int result ''resd 1
   
    val1=4
    val2=6 '

' 1) '- multiply go asm ------------------- //     
'
mov eax,val1 
mov eax,val2  ''multiply eax by ebx  "
mul val1 'ebx 
mov result,eax

'- multiply go asm ------------------- //

'
' 2) how I can add the result of "mov result,eax" to the Call MessageBox  ?
'------------ messagebox go ---------- //
'32bit asm
push 2 ' 0 '2
addr eax,tit
push eax
addr eax,msg
push eax
push 0
call MessageBox ' here should be the result of multiply mov result,eax

'------------ messagebox go ---------- //

print "the result of val1*val2 is " + str(result) '24


Frank Brübach

#4
Found the solution :)
Wanted only to make this example for using a Powerbasic a Like Style with Message Box and a result of two values Here multiplication

Oxygen basic

' multiplication of two values
'
Declare Function MessageBox Lib "user32.dll" alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long



' 1) '- multiply go asm ------------------- //     
'

byte val1
byte val2
int result ''resd 1
   
    val1=4
    val2=6 '

     mov eax,val1 
     mov eax,val2  ''multiply eax by ebx  "
     mul val1 'ebx 
     mov result,eax

     
'print "the result of val1*val2 is " + str(result) '24


zstring tit[] = " PowerBASIC"
string msg[] = " hello my powerbasic friends " + str(result)

push 2 ' 0
addr eax,tit
push eax
addr eax,msg
push eax
addr eax,result
push 0

call MessageBox
mov eax, msg