Jump to content
Tuts 4 You

Python to Assembly code


tarequl.hassan

Recommended Posts

tarequl.hassan
Posted

Hi All

 

What could be the code in Assembly for the below Python code?

Result =str(random.randint(800000,899999))

 

This is just to get random number of 6 digit between 800000 to 899999.

 

Thanks

  • Like 1
  • Thanks 1
Posted (edited)
RandomRange proc uses esi edi ebx _RangeLow:dword, _RangeHigh:dword
	
	mov 	esi,_RangeHigh 
	mov 	ebx,_RangeLow
	.if esi < ebx
		mov 	eax,ebx
		sub 	eax,esi
		call 	RandomInt
		add 	eax,esi
	.else
		mov 	eax,esi
		sub 	eax,ebx
		call 	RandomInt                      
		add 	eax,ebx
	.endif
	
	ret
RandomRange endp

RandomInt Proc uses ebx

	push 	ebx                             
	xor 	ebx,ebx
	imul 	edx,dword ptr[ebx+Seed],08088405h
	inc 	edx
	mov 	dword ptr[ebx+Seed],edx
	mul 	edx
	mov 	eax,edx
	pop 	ebx
	ret                                  
RandomInt endp

how to use:

push HighValue
push LowValue
call RandomRange

Random Value is in returned in eax, use wsprintf to make a string.

enjoy

Edited by sama
  • Thanks 1
Posted
8 minutes ago, sama said:

RandomRange proc uses esi edi ebx _RangeLow:dword, _RangeHigh:dword
	
	mov 	esi,_RangeHigh 
	mov 	ebx,_RangeLow
	.if esi < ebx
		mov 	eax,ebx
		sub 	eax,esi
		call 	RandomInt
		add 	eax,esi
	.else
		mov 	eax,esi
		sub 	eax,ebx
		call 	RandomInt                      
		add 	eax,ebx
	.endif
	
	ret
RandomRange endp

RandomInt Proc uses ebx

	push 	ebx                             
	xor 	ebx,ebx
	imul 	edx,dword ptr[ebx+Seed],08088405h
	inc 	edx
	mov 	dword ptr[ebx+Seed],edx
	mul 	edx
	mov 	eax,edx
	pop 	ebx
	ret                                  
RandomInt endp

enjoy

Hi Sama

The above code generates serial with 7 digit, but the requirement is 6 digit.

 

Am I ok?

 

 

Posted (edited)

how did you check it ?

spacer.png

Edited by sama
Posted
39 minutes ago, sama said:

how did you check it ?

spacer.png

Oops I added one digit more!!!😀

Posted

Knowledge that will always work is to write and translate in a programming language you know. For example, a conversion attempt from Delphi;

Function SayiUret(RandMin, RandMax: Integer): Integer;
Var
RandRange: Integer;
RandValue: Integer;
Begin
If RandMax <= RandMin Then
Begin
Result := RandMin;
Exit;
End;
Randomize;
RandRange := RandMax-RandMin;
RandValue := Random(RandRange);
Result := RandValue + RandMin;
End;

procedure TForm2.Button1Click(Sender: TObject);
var
x:integer;
begin
x:=sayiuret(800000,899999);
edit1.text:=(inttostr(x));
end;

 

"Base.bat" and "rsrc.rc"

Base.bat

;@echo off
;goto KesMezar

;for tarequl.hassan
;899999 - 800000 random
.686
.model	flat, stdcall
option	casemap :none   ; case sensitive

include	\MASM32\INCLUDE\windows.inc

uselib	MACRO	libname
	include		\MASM32\INCLUDE\libname.inc
	includelib		\MASM32\LIB\libname.lib
ENDM

uselib	user32
uselib	kernel32


DlgProc		PROTO :DWORD,:DWORD,:DWORD,:DWORD
Generate		PROTO :DWORD,:DWORD
Randomize PROTO
Random PROTO ;:DWORD
_TForm2_Button1Click PROTO :HWND


IDC_OK 			equ	1003
IDC_IDCANCEL 	equ	1004

.data
Format        db    "%u",0

max dd 0DBB9Fh;899999
min dd 0C3500h;800000

deger1 		dd 0
 
.data?
hInstance	dd  ?
hafiza      dd  ?
Sonuc 		dd  ?

.code
start:
	invoke	GetModuleHandle, NULL
	mov	hInstance, eax
	invoke	DialogBoxParam, hInstance, 101, 0, ADDR DlgProc, 0
	invoke	ExitProcess, eax
; -----------------------------------------------------------------------
DlgProc	proc	hWin	:DWORD,
		uMsg	:DWORD,
		wParam	:DWORD,
		lParam	:DWORD

	.if	uMsg == WM_COMMAND
		.if	wParam == IDC_OK


		
			;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		invoke _TForm2_Button1Click,hWin
			;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		
		.endif

        ;.elseif	wParam == IDC_IDCANCEL
		;	invoke EndDialog,hWin,0
		;.endif
	.elseif	uMsg == WM_CLOSE
		invoke	EndDialog,hWin,0
	.endif

	xor	eax,eax
	ret
DlgProc	endp



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Randomize proc
;local SystemTime:SYSTEMTIME	
;invoke GetSystemTime, addr SystemTime	
;movzx eax,[SystemTime.wHour]	
;imul eax,60	
;add ax,[SystemTime.wMinute]	
;imul eax,60	
;xor edx,edx	
;mov dx,[SystemTime.wSecond]	
;add eax,edx	
;imul eax,1000	
;mov dx,[SystemTime.wMilliseconds]	
;add eax, edx	
;mov deger1,eax
;ret
;Randomize endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Random proc
;rdtsc
;mul edx
;mov eax,edx	
;ret
;Random endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Randomize proc
LOCAL A:DWORD
                 call    QueryPerformanceCounter
                 test    eax, eax
                 jz      short loc_404750
                 mov     eax, [esp+8+A]
                 mov     deger1, eax
                 pop     ecx
                 pop     edx
                 ret
loc_404750:
                 call    GetTickCount
                 mov     deger1, eax
				 Ret
Randomize endp


Random proc
push    ebx
xor     ebx, ebx
imul    edx, deger1, 8088405h
inc     edx
mov     deger1, edx
mul     edx
mov     eax, edx
pop     ebx
ret
Random endp


Generate proc RandMin:DWORD,RandMax:DWORD
LOCAL RandRange:DWORD
LOCAL RandValue:DWORD
                mov     RandMax,edx
                mov     RandMin,eax
                mov     eax, RandMax
                cmp     eax, RandMin
                jg      short loc_4B1044
                mov     eax, RandMin
                mov     hafiza,eax
                jmp     short loc_4B1070
loc_4B1044:
                call    Randomize
                mov     eax, RandMax
                sub     eax, RandMin
                mov     RandRange,eax
                call    Random
                mov     RandValue,eax
                mov     eax, RandValue
                add     eax, RandMin
                mov     hafiza,eax
loc_4B1070:           
                mov     eax, hafiza               
                ret
Generate endp


_TForm2_Button1Click proc hWin:HWND
                mov     edx, max
                mov     eax, min
                invoke Generate,eax,edx
                invoke wsprintf,addr Sonuc,addr Format,eax                
                invoke SetDlgItemText,hWin,1001,addr Sonuc
                xor     eax, eax				
                ret
_TForm2_Button1Click endp

end start



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
:KesMezar
set dosya=base
if not exist rsrc.rc goto KesMezarDerle
\MASM32\BIN\Rc.exe /v rsrc.rc
\MASM32\BIN\Cvtres.exe /machine:ix86 rsrc.res
:KesMezarDerle
if exist %dosya%.obj del %dosya%.obj
if exist %dosya%.exe del %dosya%.exe
\MASM32\BIN\Ml.exe /c /coff %dosya%.bat
\MASM32\BIN\Link.exe /SUBSYSTEM:WINDOWS /opt:nowin98  /MERGE:.rdata=.text -ignore:4078 %dosya%.obj rsrc.obj
del %dosya%.obj
del rsrc.obj
del rsrc.RES
pause
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

rsrc.rc

#include "\MASM32\INCLUDE\RESOURCE.H"
#define IDC_OK 1003

101 DIALOGEX 0,0,149,20
CAPTION "for tarequl.hassan"
FONT 8,"Tahoma"
STYLE 0x80c80880
EXSTYLE 0x00000000
BEGIN
	CONTROL "OK",IDC_OK,"Button",0x10000001,97,3,50,14,0x00000000
	CONTROL "",1001,"Edit",0x10000080,3,3,90,12,0x00000200
END

 

  • Like 1
Posted

You could have used RandomRange for your function to change from...

Function SayiUret(RandMin, RandMax: Integer): Integer;
Var
	RandRange: Integer;
	RandValue: Integer;
Begin
	If RandMax <= RandMin Then
	Begin
		Result := RandMin;
		Exit;
	End;
	Randomize;
	RandRange := RandMax-RandMin;
	RandValue := Random(RandRange);
	Result := RandValue + RandMin;
End;

To...

Function SayiUret(RandMin, RandMax: Integer): Integer;
Begin
	Randomize;
	Result := RandomRange(RandMin, RandMax);
End;

IMO it makes it more readable and user friendly and although I haven't checked probably would have compiled to more efficient code

:)

Posted (edited)

Thanks. The purpose was to stretch the code a little and show the change.

It's shorter now;

uses
math;

procedure TForm2.Button3Click(Sender: TObject);
var
i:integer;
begin
randomize;
i:=RandomRange(800000,899999);
edit3.text:=inttostr(i);
end;

P.S: There is no innovation for assembly.

.text:004B106F                 call    @System@Randomize$qqrv ; System::Randomize(void) -> randomize
.text:004B1074                 mov     edx, 0DBB9Fh
.text:004B1079                 mov     eax, 0C3500h
.text:004B107E                 call    @Math@RandomRange$qqrxixi ; Math::RandomRange(int,int)->>>2 x Random
.text:004B1083                 mov     [ebp+var_8], eax

 

Edited by k3s_m3z4r
Posted
17 hours ago, tarequl.hassan said:

What could be the code in Assembly for the below Python code?

 

To answer this a bit more specifically, Python is not compiled down into assembly/machine code. It is an interpreted language that is 'compiled' into it's own byte code that is specific to Python and its interpreter. Using any bundler/tool that converts a script to an exe or similar is just bundling it into a wrapped pre-made executor. This is not actually compiling down to real ASM still, and your script can be dumped back to source from the exe generated. 

  • Thanks 1
tarequl.hassan
Posted
23 hours ago, k3s_m3z4r said:

Knowledge that will always work is to write and translate in a programming language you know. For example, a conversion attempt from Delphi;


Function SayiUret(RandMin, RandMax: Integer): Integer;
Var
RandRange: Integer;
RandValue: Integer;
Begin
If RandMax <= RandMin Then
Begin
Result := RandMin;
Exit;
End;
Randomize;
RandRange := RandMax-RandMin;
RandValue := Random(RandRange);
Result := RandValue + RandMin;
End;

procedure TForm2.Button1Click(Sender: TObject);
var
x:integer;
begin
x:=sayiuret(800000,899999);
edit1.text:=(inttostr(x));
end;

 

"Base.bat" and "rsrc.rc"

Base.bat


;@echo off
;goto KesMezar

;for tarequl.hassan
;899999 - 800000 random
.686
.model	flat, stdcall
option	casemap :none   ; case sensitive

include	\MASM32\INCLUDE\windows.inc

uselib	MACRO	libname
	include		\MASM32\INCLUDE\libname.inc
	includelib		\MASM32\LIB\libname.lib
ENDM

uselib	user32
uselib	kernel32


DlgProc		PROTO :DWORD,:DWORD,:DWORD,:DWORD
Generate		PROTO :DWORD,:DWORD
Randomize PROTO
Random PROTO ;:DWORD
_TForm2_Button1Click PROTO :HWND


IDC_OK 			equ	1003
IDC_IDCANCEL 	equ	1004

.data
Format        db    "%u",0

max dd 0DBB9Fh;899999
min dd 0C3500h;800000

deger1 		dd 0
 
.data?
hInstance	dd  ?
hafiza      dd  ?
Sonuc 		dd  ?

.code
start:
	invoke	GetModuleHandle, NULL
	mov	hInstance, eax
	invoke	DialogBoxParam, hInstance, 101, 0, ADDR DlgProc, 0
	invoke	ExitProcess, eax
; -----------------------------------------------------------------------
DlgProc	proc	hWin	:DWORD,
		uMsg	:DWORD,
		wParam	:DWORD,
		lParam	:DWORD

	.if	uMsg == WM_COMMAND
		.if	wParam == IDC_OK


		
			;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		invoke _TForm2_Button1Click,hWin
			;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		
		.endif

        ;.elseif	wParam == IDC_IDCANCEL
		;	invoke EndDialog,hWin,0
		;.endif
	.elseif	uMsg == WM_CLOSE
		invoke	EndDialog,hWin,0
	.endif

	xor	eax,eax
	ret
DlgProc	endp



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Randomize proc
;local SystemTime:SYSTEMTIME	
;invoke GetSystemTime, addr SystemTime	
;movzx eax,[SystemTime.wHour]	
;imul eax,60	
;add ax,[SystemTime.wMinute]	
;imul eax,60	
;xor edx,edx	
;mov dx,[SystemTime.wSecond]	
;add eax,edx	
;imul eax,1000	
;mov dx,[SystemTime.wMilliseconds]	
;add eax, edx	
;mov deger1,eax
;ret
;Randomize endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Random proc
;rdtsc
;mul edx
;mov eax,edx	
;ret
;Random endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Randomize proc
LOCAL A:DWORD
                 call    QueryPerformanceCounter
                 test    eax, eax
                 jz      short loc_404750
                 mov     eax, [esp+8+A]
                 mov     deger1, eax
                 pop     ecx
                 pop     edx
                 ret
loc_404750:
                 call    GetTickCount
                 mov     deger1, eax
				 Ret
Randomize endp


Random proc
push    ebx
xor     ebx, ebx
imul    edx, deger1, 8088405h
inc     edx
mov     deger1, edx
mul     edx
mov     eax, edx
pop     ebx
ret
Random endp


Generate proc RandMin:DWORD,RandMax:DWORD
LOCAL RandRange:DWORD
LOCAL RandValue:DWORD
                mov     RandMax,edx
                mov     RandMin,eax
                mov     eax, RandMax
                cmp     eax, RandMin
                jg      short loc_4B1044
                mov     eax, RandMin
                mov     hafiza,eax
                jmp     short loc_4B1070
loc_4B1044:
                call    Randomize
                mov     eax, RandMax
                sub     eax, RandMin
                mov     RandRange,eax
                call    Random
                mov     RandValue,eax
                mov     eax, RandValue
                add     eax, RandMin
                mov     hafiza,eax
loc_4B1070:           
                mov     eax, hafiza               
                ret
Generate endp


_TForm2_Button1Click proc hWin:HWND
                mov     edx, max
                mov     eax, min
                invoke Generate,eax,edx
                invoke wsprintf,addr Sonuc,addr Format,eax                
                invoke SetDlgItemText,hWin,1001,addr Sonuc
                xor     eax, eax				
                ret
_TForm2_Button1Click endp

end start



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
:KesMezar
set dosya=base
if not exist rsrc.rc goto KesMezarDerle
\MASM32\BIN\Rc.exe /v rsrc.rc
\MASM32\BIN\Cvtres.exe /machine:ix86 rsrc.res
:KesMezarDerle
if exist %dosya%.obj del %dosya%.obj
if exist %dosya%.exe del %dosya%.exe
\MASM32\BIN\Ml.exe /c /coff %dosya%.bat
\MASM32\BIN\Link.exe /SUBSYSTEM:WINDOWS /opt:nowin98  /MERGE:.rdata=.text -ignore:4078 %dosya%.obj rsrc.obj
del %dosya%.obj
del rsrc.obj
del rsrc.RES
pause
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

rsrc.rc


#include "\MASM32\INCLUDE\RESOURCE.H"
#define IDC_OK 1003

101 DIALOGEX 0,0,149,20
CAPTION "for tarequl.hassan"
FONT 8,"Tahoma"
STYLE 0x80c80880
EXSTYLE 0x00000000
BEGIN
	CONTROL "OK",IDC_OK,"Button",0x10000001,97,3,50,14,0x00000000
	CONTROL "",1001,"Edit",0x10000080,3,3,90,12,0x00000200
END

 

call QueryPerformanceCounter

test eax, eax

I am having problem here.

 

tarequl.hassan
Posted
On 2/26/2021 at 6:50 PM, sama said:

RandomRange proc uses esi edi ebx _RangeLow:dword, _RangeHigh:dword
	
	mov 	esi,_RangeHigh 
	mov 	ebx,_RangeLow
	.if esi < ebx
		mov 	eax,ebx
		sub 	eax,esi
		call 	RandomInt
		add 	eax,esi
	.else
		mov 	eax,esi
		sub 	eax,ebx
		call 	RandomInt                      
		add 	eax,ebx
	.endif
	
	ret
RandomRange endp

RandomInt Proc uses ebx

	push 	ebx                             
	xor 	ebx,ebx
	imul 	edx,dword ptr[ebx+Seed],08088405h
	inc 	edx
	mov 	dword ptr[ebx+Seed],edx
	mul 	edx
	mov 	eax,edx
	pop 	ebx
	ret                                  
RandomInt endp

how to use:


push HighValue
push LowValue
call RandomRange

Random Value is in returned in eax, use wsprintf to make a string.

enjoy

What is the seed?

Posted (edited)

 Is the problem moving the code?

Just try this;

(compatible with "generate proc")

Randomize proc
                 call    GetTickCount
                 mov     deger1, eax
                 Ret
Randomize endp
Random proc
	rdtsc
	mul edx
	mov eax,edx	
	ret
Random endp

http://masm32.com/board/index.php?topic=838.0 ->can be examined in more detail.

Edited by k3s_m3z4r
  • Thanks 1
tarequl.hassan
Posted

.Data

alphabet db "0123456789", 0

xor esi, esi

nextChar:

invoke nrandom, 10

movzx edx, byte ptr alphabet[eax]

mov byte ptr serial[esi], dl

shl edx, 4

add ebx, edx

inc esi

cmp esi, 8

 jb nextChar

Mov byte ptr ds: [serial], 38h

invoke SetDlgItemText,hWin,1001,addr serial

This also works!!!

Posted
21 hours ago, tarequl.hassan said:

What is the seed?

a global variable of dwordsize

.data?
Seed 	dd ?

 

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...