algorithm into assembly language

1) Translate the following algorithm into assembly language.
IF X > 12 THEN X = 2*X + 4 ELSE X = X + Y
Does anyone know how to do this..
i know I have to star with
CMP 12,X
Then do I store that somewhere??
if(x > 12) {
X = 2 * X + 4
}
else{
X += Y
}

Is that what you were looking for?
What assembly language? There is MIPS, Intel, etc. Which one are you trying to convert it to?
writing it like this
mov( a, eax );

cmp( eax, b );

jne DoIF;
Looks like intel code. Using gcc, I was able to generate this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
	.file	"file.c"
	.intel_syntax noprefix
	.def	___main;	.scl	2;	.type	32;	.endef
	.text
	.globl	_main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
LFB0:
	.cfi_startproc
	push	ebp
	.cfi_def_cfa_offset 8
	.cfi_offset 5, -8
	mov	ebp, esp
	.cfi_def_cfa_register 5
	and	esp, -16
	sub	esp, 16
	call	___main
	mov	DWORD PTR [esp+12], 12
	mov	DWORD PTR [esp+8], 3
	cmp	DWORD PTR [esp+12], 12
	jle	L2
	mov	eax, DWORD PTR [esp+12]
	add	eax, eax
	add	eax, 4
	mov	DWORD PTR [esp+12], eax
	jmp	L3
L2:
	mov	eax, DWORD PTR [esp+8]
	add	DWORD PTR [esp+12], eax
L3:
	mov	eax, 0
	leave
	.cfi_restore 5
	.cfi_def_cfa 4, 4
	ret
	.cfi_endproc
LFE0:
	.ident	"GCC: (GNU) 4.8.1"


But it looks a little to long for something simple

original code:
1
2
3
4
5
6
7
8
int main() {
    int x = 12, y = 3;
    if (x > 12) {
        x = 4 + (x << 1);
    }
    else x += y;
    return 0;    
}
Last edited on
Topic archived. No new replies allowed.