User:NorwegianBlue/Assembly example
C version
#include <stdio.h>
#define LOLIM 10000
#define HILIM 100000
#define TWO_POW_31 2147483648
int MyRandom()
{
static unsigned int prev = 0;
prev = (1664525*prev + 1013904223)%TWO_POW_31;
return ((prev/4) % (HILIM-LOLIM));
}
int main()
{
FILE* f;
int i,t[HILIM];
for (i = 0; i < HILIM; ++i)
{
t[i] = LOLIM+MyRandom();
}
f = fopen("c:\\silly_example.bin", "wb");
if (f != 0)
{
fwrite(t, sizeof(int), HILIM, f);
fclose(f);
}
return 0;
}
Assembly version
TITLE E:\src\masm_test\main.c
.386P
include listing.inc
if @Version gt 510
.model FLAT
else
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
_DATA SEGMENT DWORD USE32 PUBLIC 'DATA'
_DATA ENDS
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
_BSS SEGMENT DWORD USE32 PUBLIC 'BSS'
_BSS ENDS
$$SYMBOLS SEGMENT BYTE USE32 'DEBSYM'
$$SYMBOLS ENDS
$$TYPES SEGMENT BYTE USE32 'DEBTYP'
$$TYPES ENDS
_TLS SEGMENT DWORD USE32 PUBLIC 'TLS'
_TLS ENDS
; COMDAT ??_C@_02NGAF@wb?$AA@
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
; COMDAT ??_C@_0BF@CELN@c?3?2silly_example?4bin?$AA@
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
; COMDAT _MyRandom
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
; COMDAT _main
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif
_BSS SEGMENT
_?prev@?1??MyRandom@@9@9 DD 01H DUP (?)
_BSS ENDS
PUBLIC _MyRandom
; COMDAT _MyRandom
_TEXT SEGMENT
_MyRandom PROC NEAR ; COMDAT
; 9 : {
push ebp
mov ebp, esp
sub esp, 64 ; 00000040H
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-64]
mov ecx, 16 ; 00000010H
mov eax, -858993460 ; ccccccccH
rep stosd
; 10 : static unsigned int prev = 0;
; 11 : prev = (1664525*prev + 1013904223)%TWO_POW_31;
mov eax, DWORD PTR _?prev@?1??MyRandom@@9@9
imul eax, 1664525 ; 0019660dH
add eax, 1013904223 ; 3c6ef35fH
xor edx, edx
mov ecx, -2147483648 ; 80000000H
div ecx
mov DWORD PTR _?prev@?1??MyRandom@@9@9, edx
; 12 : return ((prev/4) % (HILIM-LOLIM));
mov eax, DWORD PTR _?prev@?1??MyRandom@@9@9
shr eax, 2
xor edx, edx
mov ecx, 90000 ; 00015f90H
div ecx
mov eax, edx
; 13 : }
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret 0
_MyRandom ENDP
_TEXT ENDS
PUBLIC _main
PUBLIC ??_C@_02NGAF@wb?$AA@ ; `string'
PUBLIC ??_C@_0BF@CELN@c?3?2silly_example?4bin?$AA@ ; `string'
EXTRN _fclose:NEAR
EXTRN _fopen:NEAR
EXTRN _fwrite:NEAR
EXTRN __chkstk:NEAR
EXTRN __chkesp:NEAR
; COMDAT ??_C@_02NGAF@wb?$AA@
; File E:\src\masm_test\main.c
CONST SEGMENT
??_C@_02NGAF@wb?$AA@ DB 'wb', 00H ; `string'
CONST ENDS
; COMDAT ??_C@_0BF@CELN@c?3?2silly_example?4bin?$AA@
CONST SEGMENT
??_C@_0BF@CELN@c?3?2silly_example?4bin?$AA@ DB 'c:\silly_example.bin', 00H ; `string'
CONST ENDS
; COMDAT _main
_TEXT SEGMENT
_f$ = -4
_i$ = -8
_t$ = -400008
_main PROC NEAR ; COMDAT
; 17 : {
push ebp
mov ebp, esp
mov eax, 400072 ; 00061ac8H
call __chkstk
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-400072]
mov ecx, 100018 ; 000186b2H
mov eax, -858993460 ; ccccccccH
rep stosd
; 18 : FILE* f;
; 19 : int i,t[HILIM];
; 20 : for (i = 0; i < HILIM; ++i)
mov DWORD PTR _i$[ebp], 0
jmp SHORT $L795
$L796:
mov eax, DWORD PTR _i$[ebp]
add eax, 1
mov DWORD PTR _i$[ebp], eax
$L795:
cmp DWORD PTR _i$[ebp], 100000 ; 000186a0H
jge SHORT $L797
; 22 : t[i] = LOLIM+MyRandom();
call _MyRandom
add eax, 10000 ; 00002710H
mov ecx, DWORD PTR _i$[ebp]
mov DWORD PTR _t$[ebp+ecx*4], eax
; 23 : }
jmp SHORT $L796
$L797:
; 24 : f = fopen("c:\\silly_example.bin", "wb");
push OFFSET FLAT:??_C@_02NGAF@wb?$AA@ ; `string'
push OFFSET FLAT:??_C@_0BF@CELN@c?3?2silly_example?4bin?$AA@ ; `string'
call _fopen
add esp, 8
mov DWORD PTR _f$[ebp], eax
; 25 : if (f != 0)
cmp DWORD PTR _f$[ebp], 0
je SHORT $L800
; 27 : fwrite(t, sizeof(int), HILIM, f);
mov edx, DWORD PTR _f$[ebp]
push edx
push 100000 ; 000186a0H
push 4
lea eax, DWORD PTR _t$[ebp]
push eax
call _fwrite
add esp, 16 ; 00000010H
; 28 : fclose(f);
mov ecx, DWORD PTR _f$[ebp]
push ecx
call _fclose
add esp, 4
$L800:
; 30 : return 0;
xor eax, eax
; 31 : }
pop edi
pop esi
pop ebx
add esp, 400072 ; 00061ac8H
cmp ebp, esp
call __chkesp
mov esp, ebp
pop ebp
ret 0
_main ENDP
_TEXT ENDS
END
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.