Converting a C++ Program to x86 Assembly
INCLUDE Irvine32.inc
.DATA
.CODE
;-------------------------------------
; Function
; int main()
;-------------------------------------
main PROC
mov ebx, 1 ; ebx = 1;
_while1: ; while (ebx < 1400) {
cmp ebx, 1400
jge _end_while1
mov eax, ebx ; cout << ebx;
call WriteDec
mov eax, ',' ; cout << ',';
call WriteChar
mov eax, ' ' ; cout << ' ';
call WriteChar
mov ecx, 1 ; ecx = 1;
mov esi, ebx ; esi = ebx;
_while2: ; while (esi > 0) {
cmp esi, 0
jle _end_while2
mov eax, 10 ; edi = Modulus(esi, 10);
push eax
push esi
call Modulus
add esp, 8
mov edi, eax
_if1: ; if (edi != 0) {
cmp edi, 0
je _end_if1
push edi ; ecx = Multiplication(ecx, edi);
push ecx
call Multiplication
add esp, 8
mov ecx, eax
; }
_end_if1:
mov eax, 10 ; esi = Division(esi, 10);
push eax
push esi
call Division
add esp, 8
mov esi, eax
jmp _while2 ; }
_end_while2:
add ebx, ecx ; ebx += ecx;
jmp _while1 ; }
_end_while1:
call CrLf
call WaitMsg ; system("PAUSE");
mov eax, 0 ; return 0;
ret
main ENDP
;-------------------------------------
; Function
; int Multiplication(int eax, int ebx)
;-------------------------------------
Multiplication PROC
push ebp
mov ebp, esp
push ebx
mov eax, [ebp + 8]
mov ebx, [ebp + 12]
mul ebx ; eax = eax * ebx
pop ebx
pop ebp
ret ; return eax;
Multiplication ENDP
;-------------------------------------
; Function
; int Division(int eax, int ebx)
;-------------------------------------
Division PROC
push ebp
mov ebp, esp
push ebx
mov eax, [ebp + 8]
mov ebx, [ebp + 12]
mov edx, 0 ; clear edx before division
div ebx ; eax = eax / ebx;
pop ebx
pop ebp
ret ; return eax;
Division ENDP
;-------------------------------------
; Function
; int Modulus(int eax, int ebx)
;-------------------------------------
Modulus PROC
push ebp
mov ebp, esp
push ebx
mov eax, [ebp + 8]
mov ebx, [ebp + 12]
mov edx, 0 ; clear edx before division
div ebx ; eax = eax % ebx;
mov eax, edx ; put remainder in eax
pop ebx
pop ebp
ret ; return eax;
Modulus ENDP
END main