
#include "ass.h"

operand2(mode)
register int *mode;
{
    extern indx,buffered;
    extern char line[];
    
    register r;    
    int val,rel,def;
    char tok[15];

    if ((r=reg()) != -1)
    {
        *mode=0;
        return r;
    }
    if (line[indx] == '(')
    {
        r=brackreg();
        if (line[indx] == '+')
        {
            *mode=2;
            indx++;
        }
        else
            *mode=1;
        return r;
    }
    if (line[indx] == '#')
    {
        indx++;
        val=strexpr(tok,&rel,&def);
        *mode=2;
        buffered=TRUE;
        putexpr(val,tok,rel,FALSE);
        return 7;
    }
    if (line[indx] == '@')
    {
        indx++;
        if (line[indx] == '(')
        {
            r=brackreg();
            indx++;
            *mode=3;
            return r;
        }
        if (line[indx]=='-' && line[indx+1]=='(')
        {
            indx++;
            r=brackreg();
            *mode=5;
            return r;
        }
        if (line[indx] == '#')
        {
            indx++;
            val=strexpr(tok,&rel,&def);
            *mode=3;
            buffered=TRUE;
            putexpr(val,tok,rel,FALSE);
            return 7;
        }
        val=strexpr(tok,&rel,&def);
        *mode=7;
        buffered=TRUE;
        if (line[indx] == '(')
        {
            r=brackreg();
            putexpr(val,tok,rel,FALSE);
            return r;
        }
        else
        {
            putexpr(val,tok,rel,TRUE);
            return 7;
        }
    }
    if (line[indx]=='-' && line[indx+1]=='(')
    {
        indx++;
        r=brackreg();
        *mode=4;
        return r;
    }
    val=strexpr(tok,&rel,&def);
    *mode=6;
    buffered=TRUE;
    if (line[indx] == '(')
    {
        r=brackreg();
        putexpr(val,tok,rel,FALSE);
        return r;
    }
    else
    {
        putexpr(val,tok,rel,TRUE);
        return 7;
    }
}

putexpr(val,s,rel,pcrel)
int val,rel,pcrel;
register char *s;
{
    extern pc,list,prspc,buffered;
    extern FILE *listfile;
    
    static char prbuf[30]="";

    register w;
    register char *t,c;

    w=0;
    if (pcrel)
        if (*s)
            putstr('@',s);
        else
            if (rel)
                putoct(0,w=val-pc);
            else
                putoct('-',w=val-pc);
    else
        if (*s)
            putstr(0,s);
        else
            if (rel)
                putoct('+',w=val);
            else
                putoct(0,w=val);
    if (list)
    {
        if (!*s && ((pcrel && !rel) || (!pcrel && rel)))
            c='\'';
        else
            c=' ';
        if (buffered)
        {
            t=prbuf;
            while (*t)
                ++t;
            sprintf(t,"%06o%c ",w,c);
            prspc-=8;
        }
        else
        {
            if (prspc-8 >= 0)
            {
                fprintf(listfile,"%06o%c ",w,c);
                prspc-=8;
            }
            if (*prbuf)
            {
                fprintf(listfile,"%s",prbuf);
                *prbuf='\0';
            }
        }
    }
    buffered=FALSE;
}

putoct(c,w)
char c;
int w;
{
    char tok[10];
    
    stroct(tok,w);
    putstr(c,tok);
}

putbyte(val)
int val;
{
    extern list,prspc;
    extern FILE *listfile;

    static word;
    static flag=FALSE;

    if (flag)
        putoct(0,val<<8|word);
    else
        word=val&0xff;
    if (list && prspc-4>=0)
    {
        fprintf(listfile,"%03o ",val);
        prspc-=4;
    }
    flag=!flag;
}    
