// // perdiv // legge da tastiera numeri esadecimali sino ad CR // calcola quoziente e resto // stampa // 9 - 02 - 2002 // // .constant OBJREF 0x40 .end-constant .main .var a b quoziente resto .end-var start:BIPUSH 0x3D OUT BIPUSH 0x20 OUT leggi:LDC_W OBJREF INVOKEVIRTUAL getnumero ISTORE a BIPUSH 0xa OUT BIPUSH 0x3D OUT BIPUSH 0x20 OUT LDC_W OBJREF INVOKEVIRTUAL getnumero ISTORE b LDC_W OBJREF ILOAD a ILOAD b INVOKEVIRTUAL div ISTORE quoziente LDC_W OBJREF ILOAD a ILOAD b INVOKEVIRTUAL mod ISTORE resto LDC_W OBJREF ILOAD quoziente INVOKEVIRTUAL print LDC_W OBJREF ILOAD resto INVOKEVIRTUAL print GOTO start .end-main .method getnumero() .var x .end-var BIPUSH 0x0 ISTORE x geta: IN DUP BIPUSH 0xa IF_ICMPEQ return // cr -> return DUP BIPUSH 0x30 ISUB IFLT geta4 // < '0' -> no buono DUP BIPUSH 0x3a ISUB IFLT nume // < ':' -> numero DUP BIPUSH 0x41 ISUB IFLT geta4 // < 'A' -> no buono DUP BIPUSH 0x46 SWAP ISUB IFLT geta4 // > 'F' -> no buono BIPUSH 0x37 // converti carattere ISUB GOTO acc nume: DUP BIPUSH 0x30 // converti numero ISUB acc: ILOAD x // shift acc di 4 bit DUP IADD DUP IADD DUP IADD DUP IADD IADD // aggiungi ad acc ISTORE x GOTO geta geta4:POP GOTO geta return:POP ILOAD x IRETURN .end-method .method print( numero ) .var conta bit .end-var BIPUSH 0xa OUT BIPUSH 0x3D OUT BIPUSH 0x20 OUT BIPUSH 0x9 ISTORE conta BIPUSH 0x1 ISTORE bit // // copia sullo stack per 8 volte, 4 bit alla volta // sono le 8 cifre esadecimali che compongono il numero // e che alla fine vengono stampate // bit1: BIPUSH 0x0 ILOAD conta BIPUSH 0x1 ISUB DUP IFEQ pr ISTORE conta ILOAD numero ILOAD bit IAND IFEQ bit2 BIPUSH 0x1 IADD bit2: ILOAD bit DUP IADD ISTORE bit ILOAD numero ILOAD bit IAND IFEQ bit3 BIPUSH 0x2 IADD bit3: ILOAD bit DUP IADD ISTORE bit ILOAD numero ILOAD bit IAND IFEQ bit4 BIPUSH 0x4 IADD bit4: ILOAD bit DUP IADD ISTORE bit ILOAD numero ILOAD bit IAND IFEQ nuovo BIPUSH 0x8 IADD // la cifra rimane sullo stack // nuovo:ILOAD bit DUP IADD ISTORE bit GOTO bit1 pr: POP POP BIPUSH 0x9 ISTORE conta pr1: ILOAD conta BIPUSH 0x1 ISUB DUP IFEQ return ISTORE conta DUP BIPUSH 0xa ISUB IFLT prn BIPUSH 0x37 IADD OUT GOTO pr1 prn: BIPUSH 0x30 IADD OUT GOTO pr1 return:BIPUSH 0xa OUT IRETURN .end-method .method div( a, b ) .var quoziente resto .end-var BIPUSH 0x0 ISTORE quoziente ILOAD a ISTORE resto ciclo:ILOAD resto ILOAD b ISUB DUP IFLT return ISTORE resto ILOAD quoziente BIPUSH 0x1 IADD ISTORE quoziente GOTO ciclo return:POP ILOAD quoziente IRETURN .end-method .method mod( a, b ) .var resto .end-var BIPUSH 0x0 ILOAD a ISTORE resto ciclo:ILOAD resto ILOAD b ISUB DUP IFLT return ISTORE resto GOTO ciclo return:POP ILOAD resto IRETURN .end-method