Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Assembly (http://www.programmingforums.org/forum20.html)
-   -   loop problem! (http://www.programmingforums.org/showthread.php?t=11412)

sackarias Sep 27th, 2006 2:18 PM

loop problem!
 
The loop wont work? This is my first program in assembly and I'm getting frustrated as all hell. It reads the data correctly, but it just wont loop. scanf returns into %o0 the amount of pieces of data it read correctly, which when I'm testing i'm always using 2, so I can't see why it wont loop and ask for 2 more integers. As far as the math portion i'll get that done later.

help..please.

:

/****************************************************
Author: Zack Hitz
Class: CIS 235
File: Exercise2
Date: September 28, 2006
Purpose: Read two integers, compute x*x + y*y, then
        output x, y, and the answer.
****************************************************/

        .data

!String constants
        .align 4
prompt:        .asciz "Enter two integers: "
        .align 4
formati:        .asciz "%d %d"
        .align 4
formato:        .asciz "x = %d\ny = %d\nh = %d\n"

        !int x = 0;
        !int y = 0;
        !int sum = 0;
    .align 4
x:      .word 0
y:      .word 0
h:      .word 0

!void main()
!{
! printf(prompt);
! scanf(formati, &x, &y);
! sum = (x*x)+(y*y);
! printf(formato, x, y, sum);
! call exit(0);
!}

        .text
        .align 4
        .global main
main:
        save %sp, -64, %sp

        loop:
        !printf(prompt)
        sethi %hi(prompt), %o1
        call printf, 0
        or %o1, %lo(prompt), %o0

        !scanf(formati, %x, %y)
        set formati, %o0
        set x, %o1
        set y, %o2
        call scanf, 0
        nop

        ld[%o1],%o1
        ld[%o2],%o2

        sethi %hi(formato), %l1
        call printf,0
        or %l1, %lo(formato), %o0

        cmp %o0, 2
        be loop
        nop

        call exit, 0
        mov 0, %o0


sarumont Sep 27th, 2006 4:12 PM

Quote:

Originally Posted by sackarias (Post 115108)
:

        be loop

Not being fluent in x86 ASM, does the 'be' instruction exist? That looks like where you are attempting to branch back to 'loop', but I cannot (quickly) find a reference to that instruction.

sackarias Sep 27th, 2006 5:08 PM

It does and I got it to work now. Thanks a lot.

big_k105 Sep 27th, 2006 5:11 PM

What was the problem with your code, if you don't mind me asking? Maybe it could help others out. :)

sackarias Sep 27th, 2006 6:23 PM

Not at all..
:

main:
        save %sp, -64, %sp

        loop:       
                !printf(prompt)
                sethi %hi(prompt), %o1
                call printf, 0
                or %o1, %lo(prompt), %o0

                !scanf(formati, %x, %y)
                set formati, %o0
                set x, %o1
                set y, %o2
                call scanf, 0
                nop
                               

                !basically, branch not equals, so you want to be done
                !otherwise, it will run through the code, hitting the end
                !and going right back to 'loop'


                !while(scanf(formati, %x, %y) == 2)
                cmp %o0,2
                bne done
                nop

                !code was here
               
                !printf(formato, %o1, %o2, %o3)
                sethi %hi(formato), %l1
                call printf,0
                or %l1, %lo(formato), %o0       

        ba loop
        nop
done:
        call exit, 0
        mov 0, %o0



All times are GMT -5. The time now is 3:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC