| Refresh | Home EGTry.com

print the values on the operand stack


print the top value of the stack

			//MethodVisitor mv=classWriter.visitMethod ..

			mv.visitInsn(ICONST_3); //put 3 onto the stack
			
			//print the top item of the operand stack
			mv.visitInsn(DUP); //duplicat the top value so we only work on the copy
			mv.visitFieldInsn(GETSTATIC,"java/lang/System", "out", "Ljava/io/PrintStream;");//put System.out to operand stack
			mv.visitInsn(SWAP); // swap of the top two values of the opestack: value1 value2 => value2 value1
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(I)V");


print two values of the stack

			mv.visitInsn(ICONST_3); 
			mv.visitInsn(ICONST_5);
			
			//work on the first two values
			mv.visitInsn(DUP2); 
			
			//print the top values and remove from the stack
			mv.visitFieldInsn(GETSTATIC,"java/lang/System", "out", "Ljava/io/PrintStream;");//put System.out to operand stack
			mv.visitInsn(SWAP);
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(I)V");
			
			//print the second value and remove from the stack
			mv.visitFieldInsn(GETSTATIC,"java/lang/System", "out", "Ljava/io/PrintStream;");//put System.out to operand stack
			mv.visitInsn(SWAP);
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(I)V");