|
|
Step by Step Example
Program to realize
|
|
Problem specification
Two integer numbers: A and B are stored in memory cells with addresses 30 and 38. Our program must compare these values and print to screen the results. For example, if A=3 and B=6 the output must be
compare result: A<B
The solution
Our program extracts A to register $1, B to $2 and then compares them. MMIX instruction CMP has a remarkable feature to return serial values -1, 0 and 1 when the result is <0, =0 and >0 respectively. So we just have to add constant 3D to this result in order to get the code of required symbol of inequality: '<', '=' or '>'.
To print the results of our program to console we'll use a special system input/output TRAP with 05 first parameter. The second operand of such instruction in E-MMI defines the number of input/output function. In discussing problem I used two kinds of output functions: 02 - print symbol to console and 09 - print string to console. The last parameter of TRAP 5 instruction is a register number that specify symbol or string address.
Described above organization of output is the specific feature of E-MMI project.
address |
code |
mnemonics |
operation |
comments |
0 | E3 00 0040 | SETL $00, 0040 | $0 <== 40 | set $0 equal to address of the text |
4 | 00 0509 00 | TRAP 5/09, $00 | print string | call system function: print string |
8 | E3 00 0030 | SETL $00, 0030 | $0 <== 30 | set $0 equal to A addres |
C | 8D 01 00 00 | LDOI $01, $00 00 | $1 <== ($0) | get 8-byte integer A value to $1 |
10 | 8D 02 00 08 | LDOI $02, $00 08 | $2 <== ($0+8) | get 8-byte integer B value to $2 |
14 | 30 00 01 02 | CMP $00, $01 $02 | $0 <== $1 cmp $2 | $0 = -1 if $1<$2, 0 if A=B, 1 if $1>$2 |
18 | 21 00 00 3D | ADDI $00, $00 3D | $0 <== $0 + 3D ('=') | $0 = '<' if $1<$2, '=' if A=B, '>' if $1>$2 |
1C | 00 0502 00 | TRAP 5/09, $00 | print symbol | call system function: print symbol |
20 | E3 00 0042 | SETL $00, 0042 | $0 <== 42 ('B') | put code of symbol 'B' to $0 |
24 | 00 0502 00 | TRAP 5/09, $00 | print symbol | call system function: print symbol |
28 | 00 000000 | TRAP 0 | exit to system | |
2C | (not used) |
30 | | A |
38 | | B |
40 48 50 |
63 6F 6D 70 61 72 65 20
72 65 73 75 6C 74 3A 20
41 24 |
compare
result:
A$ | text: 'compare result: A' and ending '$' |
Related topics:
E-MMI software
|
(C) 2002, Evgeny Eremin. rEd-MMI project documentation
|
|