assembly syntax help
assembly syntax help
(OP)
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("enter a number --->");
scanf("%d", &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("%d\n", A);
}
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("enter a number --->");
scanf("%d", &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("%d\n", A);
}
RE: assembly syntax help
loop label
cx (ecx) contains number of cycles
RE: assembly syntax help
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?
"Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
RE: assembly syntax help
# 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 "is 3 = 4?\n"
$REPLY: .asciiz "NO, beq tells us so\n"
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.
RE: assembly syntax help
Anyway, the cmp,j** instructions should probably be a b** instruction for your processor. Otherwise the implementation of the structure should be similar.
"Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
RE: assembly syntax help
RE: assembly syntax help
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.
"Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
RE: assembly syntax help
Hope it Helps
Jack