Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

assembly syntax help

Status
Not open for further replies.

AsmNewb

Technical User
Oct 16, 2002
3
US
I don't know where to start, but anyway, i am new to assembly language and trying to write a basic bubble sort program. I know the algorithm to do it, but lack the syntax knowledge such as:
1. what is the command for for loops, if loops. If anyone can direct me to some reading material like assembly for dummies, it would be helpful too.

Here are the code in C and I am just trying to convert them over to assembly.
// allocate the memory size for the integer array
int A[] = new int[arraySize];

// Now, get the list of integers from the user for the specified array size.
for (i = 0; i < arraySize; i ++) {
printf(&quot;enter a number --->&quot;);
scanf(&quot;%d&quot;, &A);
}

// Now, sort the array
for (i = 0; i < (arraySize - 1); i ++) {
for (j = (i+1); j < arraySize ; j ++) {
if (A > A[j]) {
temp = A;
A = A[j];
A[j] = temp;
}
}
}

// Print out the sorted array
for (i = 0; i < arraySize; i ++) {
printf(&quot;%d\n&quot;, A);
}
 
loop is just

loop label

cx (ecx) contains number of cycles
 
If you want, you can try and look how your compiler will compile those commands into asm. Just Read The Fine Manual, you should be able to find a switch or option to tell your compiler to output a .asm file from your input C file.

The 'loop' command is useful, but of course you are limited since it must always use (e)cx. If you're nesting loops, the 'loop' command may not be practical.

Anyway... Asm is UNSTRUCTURED. It is up to YOU to put the structure into it.

(If you're using MASM, you can actually use c-like loops... but that's cheating, really, so better learn to do it 'right!')

First, you must realize that ALL structures (loops, cases, come down to ONE THING: the 'if' command.

For instance, a 'while' loop:

while(var1<128)
{stmt1; stmt2;}

is just:

whileloop:
stmt1;
stmt2;
if(var1<128) goto whileloop; /*Yeah goto is 'obsolete' in C... but not in ASM!!*/

And of course, a 'for' loop is just a 'while' loop, ne?

Now in ASM, an 'if' is performed by using a 'cmp,' then the proper 'jxx' command.

So the 'if' above would be:
cmp var1,128
jge skip ;if Greater than or Equal to, jump to skip
jmp whileloop
skip:

Ne?


&quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
well, one thing I find strange is that the assembly language syntax that are used on this forum is very different from the one I am learning to use. This is the syntax that I am familiar with:

# belove is an example of an if statement
main:
la $a0, $MSG # Put message addr in $a0
li $v0, 4 # Service = print_string
syscall

li $s0, 3 # $s0 = 3
li $s1, 4 # $s1 = 4

beq $s0, $s1, exit # if s4 = s5 then goto exit
# otherwise, continue

la $a0, $REPLY # Put message addr in $a0
li $v0, 4 # Service = print_string
syscall
exit:
j $ra # Return to caller

#
# This is a data segment
#
.rdata
$MSG: .asciiz &quot;is 3 = 4?\n&quot;
$REPLY: .asciiz &quot;NO, beq tells us so\n&quot;

maybe I am just new so I am only using simple commands

Also, does anyone have a recommedation on reading material for SPIM assembly? I am having trouble with how to store int in arrays and pulling them out.
 
Ah, I see. I tend to assume Intel 80x86/IA32 Assembly, what processor is this for? Take note that different microprocessors have different assembly instructions, which is why you can't run a Macintosh program on a PC, since they have totally different instructions.

Anyway, the cmp,j** instructions should probably be a b** instruction for your processor. Otherwise the implementation of the structure should be similar.
&quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
well, right now I am working under a windows environment as well. So the codes are being tested using an emulator called SPIM
 
A virtual processor? Or does it emulate an actual processor?

Anyway you have an example of an if command - and a while loop is simply an if command, and a for loop is simply a while loop. You should be able to program a loop already. However, you might need to have to check the peculiarities of your particular processor. For instance, your processor might have special registers for indices/addresses. &quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
For learning Assembly , the greatest book is &quot;The Art of Assembly Language&quot; . I think that you can find a copy on the net for download... A good book for begginers is the &quot;University of Guadalaraja Assembly Tutorial &quot; . This is very good for begginers.

Hope it Helps

Jack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top