×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

assembly syntax help

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);
    }

RE: assembly syntax help

loop is just

loop label

cx (ecx) contains number of cycles

RE: assembly syntax help

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?


"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

(OP)
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 "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

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.

"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

(OP)
well, right now I am working under a windows environment as well. So the codes are being tested using an emulator called SPIM

RE: assembly syntax help

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.

"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

For learning Assembly , the greatest book is "The Art of Assembly Language" . I think that you can find a copy on the net for download... A good book for begginers is the "University of Guadalaraja Assembly Tutorial " . This is very good for begginers.

  Hope it Helps

    Jack

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close