I need to translate this C code for the Sieve of Erastothenes into Assembly x86.
ID: 3666757 • Letter: I
Question
I need to translate this C code for the Sieve of Erastothenes into Assembly x86. Any help would be greatly appreciated. This is due tomorrow.
C code:
#include <stdio.h>
#include <stdlib.h>
#define limit 100 /*size of integers array*/
int main(){
unsigned long long int i,j;
int *primes;
int z = 1;
primes = malloc(sizeof(int) * limit);
for (i = 2;i < limit; i++)
primes[i] = 1;
for (i = 2;i < limit; i++)
if (primes[i])
for (j = i;i * j < limit; j++)
primes[i * j] = 0;
printf(" Prime numbers in range 1 to 100 are: ");
for (i = 2;i < limit; i++)
if (primes[i])
printf("%d ", i);
return 0;
}
Assembly:
.586
.MODEL FLAT
INCLUDE io.h
.STACK 4096
.DATA
arrayx DB 10000 DUP(?)
primes BYTE ?
size BYTE 1000
result BYTE "The prime numbers in the range 1 to 10000 are: ", 0
.CODE
main PROC
xor eax, eax ; zeros out registers
xor ebx, ebx
xor ecx, ecx
lea ebx, arrayx ;load address of array into ebx
mov ecx, 2 ; i=2
whilei: mov eax, ecx
cmp eax, size
jge endWhilei
bodyWhilei:
Explanation / Answer
Answer:
.Ltext0:
.section .rodata
.align 8
.LC0:
0000 0A507269 .string " Prime numbers in range 1 to 100 are: "
.LC1:
0027 25640A00 .string "%d "
.text
.globl main
main:
.LFB0:
.cfi_startproc
0000 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
0001 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
0004 4883EC20 subq $32, %rsp
0008 C745E401 movl $1, -28(%rbp)
000f BF900100 movl $400, %edi
0014 E8000000 call malloc
0019 488945F8 movq %rax, -8(%rbp)
001d 48C745E8 movq $2, -24(%rbp)
0025 EB1E jmp .L2
.L3:
0040 488345E8 addq $1, -24(%rbp)
0027 488B45E8 movq -24(%rbp), %rax
002b 488D1485 leaq 0(,%rax,4), %rdx
0033 488B45F8 movq -8(%rbp), %rax
0037 4801D0 addq %rdx, %rax
003a C7000100 movl $1, (%rax)
.L2:
0045 48837DE8 cmpq $99, -24(%rbp)
004a 76DB jbe .L3
004c 48C745E8 movq $2, -24(%rbp)
0054 EB5A jmp .L4
.L8:
0056 488B45E8 movq -24(%rbp), %rax
005a 488D1485 leaq 0(,%rax,4), %rdx
0062 488B45F8 movq -8(%rbp), %rax
0066 4801D0 addq %rdx, %rax
0069 8B00 movl (%rax), %eax
006b 85C0 testl %eax, %eax
006d 743C je .L5
006f 488B45E8 movq -24(%rbp), %rax
0073 488945F0 movq %rax, -16(%rbp)
0077 EB23 jmp .L6
.L7:
0097 488345F0 addq $1, -16(%rbp)
0079 488B45E8 movq -24(%rbp), %rax
007d 480FAF45 imulq -16(%rbp), %rax
0082 488D1485 leaq 0(,%rax,4), %rdx
00000000
008a 488B45F8 movq -8(%rbp), %rax
008e 4801D0 addq %rdx, %rax
0091 C7000000 movl $0, (%rax)
.L6:
009c 488B45E8 movq -24(%rbp), %rax
00a0 480FAF45 imulq -16(%rbp), %rax
00a5 4883F863 cmpq $99, %rax
00a9 76CE jbe .L7
.L5:
00ab 488345E8 addq $1, -24(%rbp)
.L4:
00b0 48837DE8 cmpq $99, -24(%rbp)
00b5 769F jbe .L8
00b7 BF000000 movl $.LC0, %edi
00bc E8000000 call puts
00c1 48C745E8 movq $2, -24(%rbp)
00c9 EB34 jmp .L9
.L11:
00cb 488B45E8 movq -24(%rbp), %rax
00cf 488D1485 leaq 0(,%rax,4), %rdx
00d7 488B45F8 movq -8(%rbp), %rax
00db 4801D0 addq %rdx, %rax
00de 8B00 movl (%rax), %eax
00e0 85C0 testl %eax, %eax
00e2 7416 je .L10
00e4 488B45E8 movq -24(%rbp), %rax
00e8 4889C6 movq %rax, %rsi
00eb BF000000 movl $.LC1, %edi
00f0 B8000000 movl $0, %eax
00f5 E8000000 call printf
.L10:
00fa 488345E8 addq $1, -24(%rbp)
.L9:
00ff 48837DE8 cmpq $99, -24(%rbp)
0104 76C5 jbe .L11
0106 B8000000 movl $0, %eax
010b C9 leave
.cfi_def_cfa 7, 8
010c C3 ret
.cfi_endproc
.LFE0:
.Letext0: