L1VM - course 05

input

Here I will show how to get input on the console.

There are three input commands:

input_i (number)
input_d (double_number)
input_s (string)

The variable inside the brackets stores the input typed in by the user. You have to take care to use the right “input” command for your input type. The input is taken after you press the “RETURN” key!

Here is a program which calculates the diameter or circumference of a circle. It uses “input_i” and “input_d” commands:

// math-circle.l1com
// calculate diameter or circumference of circle
//
#include <intr.l1h>
(main func)
	(set int64 1 zero 0)
	(set int64 1 one 1)
	(set int64 1 two 2)
	(set int64 1 three 3)
	(set double 1 diam 0.0)
	(set double 1 circ 0.0)
	(set double 1 zerod 0.0)
	(set string s menu_diamstr "1: calculate diameter of circle")
	(set string s menu_circstr "2: calculate circumference of circle")
	(set string s menu_quitstr "3: quit")
	(set string s menu_chstr "? ")
	(set string s diamstr "diameter:      ")
	(set string s circstr "circumference: ")
	(set int64 1 input 0)
	(set int64 1 f 0)
	// set constant -----------------------------------------
	(set const-double 1 m_pi@math 3.14159265358979323846)
	// -------------------------------------------------------
	(:loop)
	print_s (menu_diamstr)
	print_n
	print_s (menu_circstr)
	print_n
	print_s (menu_quitstr)
	print_n
	print_s (menu_chstr)
	// read input
	input_i (input)
	(((input three ==) f =) f if)
		// quit
		exit (zero)
	(endif)
	(((input one ==) f =) f if+)
		// reset used variables and set used registers to zero
		// no context register saving here
		(reset-reg)
		(:calc_diam !)
		(loadreg)
	(else)
		// reset used variables and set used registers to zero
		// no context register saving here
		(reset-reg)
		(:calc_circ !)
		(loadreg)
	(endif)
	print_n
	print_n
	(:loop jmp)
(funcend)
(calc_diam func)
	// calculate diameter
	(zerod circ =)
	(zerod diam =)
	print_s (circstr)
	// input double circ
	input_d (circ)
	((circ m_pi@math /d) diam =)
	print_s (diamstr)
	print_d (diam)
(funcend)
(calc_circ func)
	// calculate circumference
	(zerod diam =)
	(zerod circ =)
	print_s (diamstr)
	// input double diam
	input_d (diam)
	((diam m_pi@math *d) circ =)
	print_s (circstr)
	print_d (circ)
(funcend)

Inside the “calc_diam” function: “((circ m_pi@math /d) diam =)” the “/d” means divide a double number. Inside the “calc_circ” function: “*d” means multiply a double number.

6: strings