cpprisc16  March 16, 2017
 All Namespaces Files Functions Variables Typedefs Macros
swap.cpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 
3 /** \file swap.cpp (March 15, 2017)
4  * \brief Very simple example program.
5  *
6  * Piece of cpprisc16.
7  * https://bitbucket.org/OPiMedia/cpprisc16
8  *
9  * Simple example: swap values of two registers.
10  *
11  * GPLv3 --- Copyright (C) 2017 Olivier Pirson
12  * http://www.opimedia.be/
13  */
14 
15 #include <cstdlib>
16 
17 #include <iostream>
18 
19 #include "cpprisc16/cpprisc16.hpp"
21 
22 using namespace cpprisc16;
23 
24 
25 int
26 main() {
28  clear_memory();
29 
30  /*
31  Equivalent to this assembler program:
32  movi 1, 0x42
33  movi 2, 0x666
34 
35  // r1, r2 <-- r2, r1 (change r3)
36  add 3, 1, 0
37  add 1, 2, 0
38  add 2, 3, 0
39  */
40 
41  p_movi(1, 0x42);
42  p_movi(2, 0x666);
43 
44  // It is possible to use any C++ code and to print register(s)
45  std::cout << "Before swapping" << std::endl;
46  println_reg(1);
47  println_reg(2);
48 
49  // With only RiSC16 instructions
51  i_add(3, 1, 0);
52  i_add(1, 2, 0);
53  i_add(2, 3, 0);
54 
55  // Print # executed instructions, registers and memory (if used)
56  println_all();
57 
58 
59  // With extended instruction x_swap
61 
62  x_swap(1, 2, 3);
63 
64  // Print # executed instructions, registers and memory (if used) before end
65  p_halt();
66 
67  return EXIT_SUCCESS;
68 }
void p_movi(unsigned int result, immed_t immed)
(MOV Immediate) R[result] <– immed
Definition: cpprisc16.cpp:166
void clear_memory()
Reset to 0 all memory items and mark them as not used.
Definition: cpprisc16.cpp:191
void println_reg(unsigned int a)
Print the register R[a].
Definition: cpprisc16.cpp:294
void i_add(unsigned int result, unsigned int a, unsigned int b)
R[result] <– R[a] + R[b].
Definition: cpprisc16.cpp:51
Instructions set of RiSC16: 8 instructions i_* and 4 pseudo-instructions p_*.
void x_swap(unsigned int a, unsigned int b, unsigned int tmp)
R[a], R[b] <– R[b], R[a].
void clear_registers()
Reset to 0 all registers.
Definition: cpprisc16.cpp:206
void p_halt(bool print)
If print then call println_all()
Definition: cpprisc16.cpp:154
void println_all()
Print infos, registers and memory (if used).
Definition: cpprisc16.cpp:260
int main()
Definition: swap.cpp:26
void clear_nb_executed()
Reset the number of executed instructions.
Definition: cpprisc16.cpp:200
Extended instructions set: some extra operations x_* implemented with RiSC16.