hi guys just want to ask about how to translate this c code into assembly language:

int x = 1;
 while( x <= 3 ){
 x++;
 printf("Sorry try again!");
}

any help would be greatly appreciated!
God bless!

How do you think it would work in assembly?

what mnemonic would you use to increment a variable/register?

what mnemonic would you use to test to see if it is within bounds?

what mnemonic would you use to jump back to the beginning?

Wait, you don't like putting in any effort.... just compile that in your c compiler and open the exe in olydbg to see how it is done...

why even bother to translate it into assembly? Other than, of course, for the learning experience. Other than academic, there is no good reason to translate it.

Other than academic, there is no good reason to translate it.

Unless you're writing a C compiler. It's not unusual to have intermediate assembly language output which is subsequently fed to an existing assembler that produces the final object code.

it's for academic purpose..I don't know where to start so i guessed u might help me

Depends on the assembler. There are dozens of possibilities.

You will want at least two functions -- the main startup code and a function named printf() (not the one from standard c library, but your own very simple version). As previously mentioned there are a variety of assembly languages, so pick one the the target computer you want to use. masm and tasm are commonly used assemblers for Intel and compatible processors. Then you will want to read a good intro to assembler book. The one I learned from was written by Peter Norton but most likely out of print now.

i'm doing this in tasm..

Ok, so show is the code you have written so far.

Top: cmp x, 4	; check loop condition
     jae Next	; if false, exit loop
     inc x	; body of loop
     jmp top	; repeat the loop
     
     lea dx, @msg
     mov ah, 09h
int 21h

As I don't fully understand C, I am unsure where to print the message...

This would be my loop:

mov		cx, 3
Top:
	
	dec		cx
	jnz		Top

Next:

Right, you only want to loop 3 times. Don't use a memory variable for a loop counter, use cx, it was made for this... a "count" register

i see so you want ti print those im sorry 3 times
you dont need that jmp code, just use the LOOP command.
Limit its loop by using the mov cx, xx code where the xx = the number of loops.
Like this
As u said ur using tasm like mine,
If you know how to display a string this would do,

mov cx, 3d

a: mov ah, 9
lea dx, (the variable of the string)
int 21h

loop a

or i think you can just put that a: beside int 21h

int x = 1;
 while( x <= 3 ){
 x++;
 printf("Sorry try again!");
}
.data
Message001 db "Sorry try again!", 0
X dd 0

...	

main:
MOV X, 1
CMP X, 3
JNB Func ;jump if is greater than 3 to a function
Inc X
PUSH OFFSET Message001
CALL crt_print ;function PrintF @\masm32\include\msvcrt.inc
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.