Read From stdin in Linux Assembler

Reading from stdin means to let the user type text and to consume that text in an application as soon as the user finishes their input by typing enter. Enter will add a linefeed character in Linux

\n = 10 = 0x0A = line feed

The user input first goes into a Linux buffer. You can call a Linux function to retrieve an amount of bytes from that buffer. Once you retrieved bytes, those bytes are subtracted from the Linux buffer so it contains only the input that was not consumed yet. You should always consume the Linux Buffer completely so that it is empty. The reason is that the buffer survives function calls. When you ask the user to input new data on a new occasion, the same input buffer is used. If it was not drained, old input will be read. The second user input might goes behind the existing data. You will expect new data but you are reading the old data first! So always drain the input buffer when asking the user for input, even if you are only interested in the first n characters.

The input is read into a array variable in your application (array of consecutive bytes in the data section). The array variable has to be defined with a fixed length in assembler, e.g. you define a byte array of 100 bytes.

Two things can happen when the user types and sends the input via enter:

  1. The user input from the Linux buffer and the newline fit into the variable in it’s entirety
  2. The user input from the Linux buffer and the newline is too large to fit into the variable.

If the input fits into the buffer, you just have to call the Linux function once which will then drain the entire input buffer. If the input is too large for your variable, you have call the Linux function several times until the Linux Input buffer is empty.

Implementation wise, reading from stdin can be done via int 80h which lets an assembler application call the Linux interrupt 80h. int 80h supports several functions https://www.tutorialspoint.com/assembly_programming/assembly_system_calls.htm. You select the function by putting its id into the eax register.

Reading from stdin has the id 3. ebx remains 0, ecx contains the array variable to put the bytes into. edx contains the amount of bytes to read, which is set to the length of the array variable.

To find out how many characters really were read from the function 3, function 3 will put the amount of bytes read into eax.

The implementation here is taken from https://stackoverflow.com/questions/23468176/read-and-print-user-input-with-x86-assembly-gnu-linux

It will read the first 5 bytes into an array variable and then it will drain the Linux input buffers one byte at a time by reading bytes into a dummy character variable until it sees the newline character. The dummy character is not processed further which means all the rest of the input is just ignored by this solution. In other words this code is only interested in the first 5 bytes and it will ignore the entire rest. The program then proceeds to output the first 5 bytes before it terminates itself.

BUFFER_SIZE equ 5
LINE_FEED equ 10

global _start           ; must be declared for using gcc ???

section .data
    str: times BUFFER_SIZE db 0 ; Allocate buffer of x bytes
    lf:  db 10          ; LF line feed

section .bss
    e1_len resd 1
    dummy resd 1

section .text

_start:                 ; tell linker entry point ???
    ; https://stackoverflow.com/questions/23468176/read-and-print-user-input-with-x86-assembly-gnu-linux

; read using function 3 (sys_read)
    mov eax, 3          ; Read user input into str
    mov ebx, 0          ; |
    mov ecx, str        ; | <- destination
    mov edx, BUFFER_SIZE        ; | <- length
    int 80h             ; \

    mov [e1_len], eax   ; Store number of inputted bytes
    cmp eax, edx        ; all bytes read?
    jb .2               ; yes: ok
    mov bl, [ecx+eax-1] ; BL = last byte in buffer
    cmp bl, LINE_FEED   ; LF in buffer?
    je .2               ; yes: ok
    inc DWORD [e1_len]  ; no: length++ (include 'lf')

; drain the linux input buffer
    .1:                 ; Loop
    mov eax, 3           ; SYS_READ
    mov ebx, 0          ; EBX=0: STDIN
    mov ecx, dummy      ; pointer to a temporary buffer
    mov edx, 1          ; read one byte
    int 0x80            ; syscall
    test eax, eax       ; EOF? eax contains the amount of bytes read
    jz .2               ; yes: ok
    mov al, [dummy]     ; AL = character
    cmp al, LINE_FEED   ; character = LF
    jne .1              ; no -> next character
    .2:                 ; end of loop

; output the array variable using function 4 from int 80h (sys_write)
    mov eax, 4          ; Print 100 bytes starting from str
    mov ebx, 1          ; |
    mov ecx, str        ; | <- source
    mov edx, [e1_len]   ; | <- length
    int 80h             ; \

; return using function 1 from int 80h (sys_exit)
    mov eax, 1          ; Return
    mov ebx, 0          ; | <- return code
    int 80h             ; \
TARGET_DIR := target
MKDIR_P = mkdir -p

all: directories main

main: main.o
	ld target/main.o -o target/main

main.o: main.asm
	nasm -f elf64 main.asm -o target/main.o

clean:
	rm target/main.o target/main

# https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
.PHONY: directories
directories: ${TARGET_DIR}

${TARGET_DIR}:
	${MKDIR_P} ${TARGET_DIR}

Angular Template-Driven Forms

There are two types of forms in angular
1. reactive (or model-driven) forms
2. template-driven forms

This article is a short reminder on how to find information and on how to work with template-driven forms.

Documentation
The official angular documentation is https://angular.io/guide/forms

Prepare node
Install the latest node Long Term Support (LTS) with nvm.
nvm install --lts

Use the latest version
nvm use node --lts

Start the app
npm start

Create a test application called angular-forms
ng new angular-forms

Generate the data object that is submitted by the form
ng generate class Hero

Create a form component
An Angular form has two parts:
1. an HTML-based template
2. A component class to handle data and user interactions programmatically.

Generate the form component
ng generate component HeroForm

Update the form component’s html

<div class="container">
<h1>Hero Form</h1>
<form (ngSubmit)="onSubmit()" #heroForm="ngForm">

{{diagnostic}}

<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" [(ngModel)]="model.name" name="name" required #spy>
</div>
TODO: remove this: {{spy.className}}

<div class="form-group">
<label for="alterEgo">Alter Ego</label>
<input type="text" class="form-control" id="alterEgo" [(ngModel)]="model.alterEgo" name="alterEgo">
</div>

<div class="form-group">
<label for="power">Hero Power</label>
<select class="form-control" id="power" [(ngModel)]="model.power" name="power" required>
<option *ngFor="let pow of powers" [value]="pow">{{pow}}</option>
</select>
</div>

<button type="submit" class="btn btn-success" [disabled]="!heroForm.form.valid">Submit</button>

</form>
</div>

 

Update the app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeroFormComponent } from './hero-form/hero-form.component';

@NgModule({
declarations: [
AppComponent,
HeroFormComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

 

Update hero-form.component.ts

import { Component } from '@angular/core';

import { Hero } from '../hero';

@Component({
selector: 'app-hero-form',
templateUrl: './hero-form.component.html',
styleUrls: ['./hero-form.component.css']
})
export class HeroFormComponent {

powers = ['Really Smart', 'Super Flexible',
'Super Hot', 'Weather Changer'];

model = new Hero(18, 'Dr IQ 3000', this.powers[0], 'Chuck OverUnderStreet');

submitted = false;

onSubmit() {
console.log('Submit clicked');
console.log(JSON.stringify(this.model));

this.submitted = true;
}

// TODO: Remove this when we're done
get diagnostic() { return JSON.stringify(this.model); }
}

OnSubmit()
When the submit button is clicked, onSubmit() is called in the form component. To persist, you can create a service and send the object to the backend in json form. The backend then persists the object.

With SpringBoot, you would add a JerseyResource for the endpoint, a JPA repository for the model item and a facade and a service to save the model via the JPA repository.

Arduino and Ethernet Module

Using the Deek-Robot NANO Ethernet Shield V1.0 and the UIPEthernet library, it is possible to add Ethernet capabilities to an Arduino microcontroller. The process is outlined in this excellent article: https://www.tweaking4all.com/hardware/arduino/arduino-enc28j60-ethernet/

A webpage served by the Arduino microcontroller gives you the opportunity to provide a nice, modern user interface to the users of your arduino project from any connected device be it a mobile device such as a smartphone or a desktop PC. You can connect hardware to the arduino which you can then control based on button clicks to the web page you serve to the local network via the Arduino web page. Besides webpages, the Arduino can now provide a REST-API which you could consume from a Angular-Application. REST-APIs allow you to seamlessly integrate the Arduino-Project into existing web applications.

This post documents the individual steps I took to follow the article on tweaking4all.

I used an Arduino UNO, the Deek-Robot NANO Ethernet Shield V1.0 (contains a ENC28J60 chip), Arduino IDE 1.8.9 and the UIPEthernet Sketch from the article on tweaking4all.

First, you have to install the UIPEthernet library into your Arduino IDE. The UIPEthernet library is needed, because the ENC28J60 does not work with the standard Ethernet libraries that ship with the Arduino IDE. The Arduino IDE allows the installation of libraries from Zip-Files. The Zip-File for the UIPEthernet library is conveniently retrieved by downloading the master branch of the Github Repository as a zip file: https://github.com/UIPEthernet/UIPEthernet > Clone or Download > Download zip. Once the zip file is contained on your harddrive, you can import it via the Arduino IDE’s installation feature: In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library. At the top of the drop down list, select the option to “Add .ZIP Library”. If the installation worked, the Arduino IDE will output: Library added to your libraries. Check “include library” menu.

Secondly, wire up the Deek-Robot board to your arduino. The page tweaking4all has a nice image on which pins have to be connected to which pins on the Arduino UNO. You need ground and 5V. My board did not die on 5V so I figure it is true that the Deek-Robot board contains a voltage converter that changes 5V to 3.3V. You have to connect the pins D10, D11, D12, D13 on the Deek-Robot board to the pins 10, 11, 12, 13 on the Arduino board in the same order (D10 is connected to 10, D11 is connected to 11, …). Also, connect a Ethernet cable between the Deek-Robot board and your home network.

Thirdly, you have to set up your router in your home network. Normally, every mac address that connects to the router gets a dynamic IP-Address assigned via DHCP which is a protocol that temporarily leases IP-Addresses to devices. Because DHCP’s use case is to connect a device that only consumes services on the network, the IP-Address is dynamic and not known before the device is connected. Without retrieving the devices IP from the device itself or from your router, you have no way to connect to the device to consume its services. The Arduino sketch contains a mac Address and a fixed IP-Address because we want to connect to the Arduino via a known IP-Address. To prevent DHCP from assigning a dynamic address, a rule is added to the router that assigns a fixed IP-Address to the mac-Address from the sketch. Weather your router is able to add rules and how to add a rule, I can not tell you because I do not know all the routers. Consult your router’s manual to add a rule. Add such a rule, then update the Sketch to contain the mac- and the IP-Address specified in the rule.

As a fourth step, you can now paste the sketch for the UIPEthernet library with correct mac and IP-Address set from tweaking4all into your Arduino IDE, connect your Arduino and Upload the sketch.

Once the sketch has been succesfully uploaded, open the IP address and port 80 in a web browser on a machine that is connected to your home network. Before connecting via the browser, you can open a serial terminal to the arduino. The Arduino will output information about a established connection into the serial terminal. Your browser will execute a GET-Request towards the Arduino Server and it will retrieve the small HelloWorld HTML-page that the sketch predefines. After serving the page, the connection is terminated by the Arduino server.

That is it! Basically if you know how to connect the board, install the library, set up a DHCP rule and after reading the article on tweaking4all, you can make your Arduino available in your home network. A downside of this approach is that the Deek-Robot board uses pins 10, 11, 12, 13 on your arduino. They are blocked for other boards. The next steps would be to figure out how to serve more resources than just the hello world webpage! How would you serve several pages connected to each other via hyperlinks for example? How do you server static resources such as CSS, JS and image files? Nevertheless, connecting the Arduino to the Ethernet network, enables your Arduino projects from your smartphone! A lot is possible. Have fun with your projects and thank you for reading this article.

Flex and Bison

Purpose of Flex and Bison

Flex and Bison allow for the generation of programs that process structured data. A prime example for such programs are interpreters or compilers of programming languages. Other applications are possible such as a loader for specific file formats.

Flex is a successor of lex. They are both lexer generators.

Bison is a successor of Yacc. They are both parser generators.

A lexer reads a string of characters and given rules, returns token matched from the character stream. Token are groups of characters. The rules applied to convert characters into token are defined by the programmer. The file ending .l is usually used for flex files.

A parser takes a stream of token and given rules, recognized constructs in the language it is supposed to parse. A parser could for example recognize function definitions or definitions of if-statements. Parts or rules or entire rules can be bound to actions. As soon as a rule is recognized, the action is executed. Actions could be code generation, for example assembler code could be output when a rule was parsed.

Usage of Flex and Bison

When compiling, the first step is to use the bison parser generator on the .y file. This will generate a .c and a .h file.

The second step is to use the flex lexer generator on the .l file. The .l file has to import the .h file that was generated by the bison run in the first step. The reason for importing that generated header is that the header defines constants for token that have to be returned by the generated lexer.

This means that first the grammar rules are processed and then after that the lexer rules are processed. This is kind of backwards. One would expect that first the token are defined and then the rules are build on top of the available token. The process is more or less backwards. You defined the rules using token and at that point you do not care how the token are defined. In the second step, when you know which token the grammar rules need, you define what the token look like by designing appropriate lexer rules.

Gotchas

On MacOS you have to link agains -ll instead of -lfl

On MacOS, your parser file has to define yyerror() and yylex()

%{
   #include <stdio.h>
   void yyerror(const char* msg) {
   	  printf("bla %s\n", msg);
      fprintf(stderr, "bli %s\n", msg);
   }
   int yylex();
%}

 

HTTP HTTPS GET Requests with Poco

The standard Poco GET example has one major deficit, I can only succesfully execute a GET request against a server that supports HTTP. Most servers do not provide their content using HTTP any more. Instead a HTTPS GET request has to be executed.

In order to adjust the Poco HTTP example to use HTTPS, follow the steps outlined in https://stackoverflow.com/questions/10875938/how-to-use-openssl-in-poco-c-library-correctly

The example below is not good code at all whatsoever but it shows how to use the HTTPSClientSession instead of the HTTPClientSession to leverage the HTTPS protocol instead of the HTTP protocol.

#include <iostream>

#include "Poco/MD5Engine.h"
#include "Poco/DigestStream.h"

#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/HTTPMessage.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/StreamCopier.h"
#include "Poco/Path.h"
#include "Poco/URI.h"
#include <Poco/Exception.h>

using namespace std;

using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPSClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using Poco::StreamCopier;
using Poco::Path;
using Poco::URI;
using Poco::Exception;

int main(int argc, char** argv) {

  try {

    std::cout << "Hello world2" << std::endl;

    //URI uri("https://www.google.de:80/index.html");
    //URI uri("https://www.play-hookey.com:80/htmltest/");
    //URI uri("http://www.brainjar.com:80/java/host/test.html");

    //http://us.imdb.com/M/title-exact?Toy%20Story%20(1995)

    //URI uri("http://us.imdb.com:80/Title?Toy%20Story%20(1995)");
    //URI uri("https://www.imdb.com:443/Title?Toy%20Story%20(1995)");
    //URI uri("https://www.imdb.com/find?q=Toy%20Story%20(1995)");
    URI uri("https://www.imdb.com/find?q=Se7en%20(1995)");

    //URI uri("https://www.imdb.com:443/find?q=Toy%20Story%20(1995)");
    //URI uri("https://www.imdb.com:443/find");
    //URI uri("https://143.204.95.231:443/find");
    //URI uri("https://www.imdb.com:443");
    //URI uri("https://www.imdb.com");
    //URI uri("www.imdb.com");

    //URI uri("https://stackoverflow.com/");

    //URI uri("https://github.com/");

    // prepare path
    string path(uri.getPathAndQuery());
    if (path.empty())
      path = "/";

    std::cout << "host " << uri.getHost() << " port " << uri.getPort()
        << " path " << path << std::endl;

    HTTPRequest request(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);

    // HTTP
    //HTTPClientSession session(uri.getHost(), uri.getPort());

    // HTTPS
    const Poco::Net::Context::Ptr context = new Poco::Net::Context(
        Poco::Net::Context::CLIENT_USE, "", "", "",
        Poco::Net::Context::VERIFY_NONE, 9, false,
        "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
    HTTPSClientSession session(uri.getHost(), uri.getPort(), context);

    std::cout << "session.sendRequest" << std::endl;
    session.sendRequest(request);

    std::cout << "session.receiveResponse" << std::endl;

    HTTPResponse response;
    std::istream& rs = session.receiveResponse(response);

    std::cout << "session.getStatus" << std::endl;
    std::cout << response.getStatus() << " " << response.getReason()
        << std::endl;

    //StreamCopier::copyStream(rs, std::cout);

    std::string responseAsString(std::istreambuf_iterator<char>(rs), { });

  } catch (Exception &ex) {
    std::cerr << ex.displayText() << std::endl;
    return -1;
  }

  return 0;
}

 

Conan Package Manager for C++

https://docs.conan.io/en/latest/getting_started.html contains a good introduction to Conan.

Installing Conen on MacOS can be done via brew:

brew update
brew install conan

Conan is controlled by a file called conanfile.txt. It is comparable to a maven pom or the package.json file for the node package manager.

The conanfile.txt from the tutorial that works with CMake is duplicated below

[requires]
Poco/1.9.0@pocoproject/stable

[generators]
cmake

You can now execute conan install in the folder that contains the conanfile.txt to exceute Conan. It will install all dependencies and call all generators listed in the conanfile.txt.

If Conan fails to download precompiled binaries, sometimes it is possible to tell conan to build the depencencies from code:

conan install Poco/1.9.0@pocoproject/stable --build missing

To use Conan in combination with CMake, the conanfile.txt has to create a conanbuildinfo.cmake file using a generator for CMake. That file is then used from within CMake’s CMakeLists.txt file. When CMake builds the project, it is able to call Conan.

A CMakeLists.txt file that imports the generated conanbuildinfo.cmake is given below:

cmake_minimum_required (VERSION 2.6)

project (PocoTest)

add_definitions("-std=c++11")

include(${PROJECT_SOURCE_DIR}/build/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(PocoTest PocoTest.cpp)
target_link_libraries(PocoTest ${CONAN_LIBS})

I think the workflow now is, whenever you need a new library in your project, add the dependency to Conans conanfile.txt. Then call conan install so Conan can download and install the new dependency and also generates an updated conanbuildinfo.cmake for CMake. Then build your project using CMake to include the newly provided dependencies.

CMake

Have a CMakeLists.txt file in each directory that
contains project source code. The root folder of all the
directories containing CMakeLists.txt files is referred
to as the source directory.

The source directory along with the binary directory
constitute the set of folders that CMake performs builds
in. A out-of-source build is one where CMake is reading
from the source directory and writing artefacts to the
binary directory. This is the default. By default CMake
will not write into the source directory, only read from
it. A in-source build is where CMake is configured to
write artifacts into the source directory, in other words
where source and binary directory are the same.

out-of-source builds are easier to maintain, because by
checking in the source directory to a version control
system and leaving the binary directory unversioned,
you are sure to not commit artifacts. Also you can
erase the binary directory and clean your project that
way.

CMakeLists.txt files contain one or more CMake commands

A command has the syntax: command (args…) where
command is the name of a command and args is a white-
space separated list of arguments. (Arguments with
embedded white-space should be double quoted).
Commands are also used for controlling the program flow
if() else() endif()

Variables are defined using the ${VAR} syntax.
Assigning a value is done using set(Foo a b c) which
sets the variable Foo to the list of values a b and c.
Using a variable: command(${Foo}) which is equivalent to
command(a b c) if Foo has the values a b c set.

Environment variables can be used: $ENV{VAR}

Building from the command line:
CMake offers the –build option, which is described as a
convenience that allows you to build your project from the
command line even if this requires launching an IDE.
The syntax is:
cmake –build <dir> [options] [– [native-options]]
cmake –build . — -v
will build in the current working directory and pass the
parameter -v to the underlying build tool.

Open Questions

Can I build with CMake?


Q: If CMake is a tool to generate build configurations
for different platforms, why can eclipse use cmake to
directly build a binary for my project?
A: CMake offers the –build option, which is described as a
convenience that allows you to build your project from the
command line even if this requires launching an IDE

Where does the build folder and the contained folders
in my EclipseCDT project come from?


Q: When looking at my CMake project in EclipseCDT, it
has a build folder and a lot of folders and files below
it, where do these folders come from?
A: ???

Eclipse CDT C++ CMake on Mac

Using Eclipse CDT on mac along with the g++ compiler is a very good option if you know your Eclipse hotkeys from programming in Java for example or if you are looking for an alternative to XCode.

Eclipse CDT allows you to create CMake projects. This post sums up the traps I got caught up in before getting everything to work in the hopes it will help others to not make the errors I made.

On your Mac, first install cmake and ninja

brew update
brew install cmake
brew install cask cmake
brew install ninja

If the brew commands fail, try repeating them, this sometimes fixes install issues.

Next, start Eclipse CDT from the console instead of from the finder (CMD + space). To start Eclipse CDT from a console, execute the command

open -a EclipseCDT.app

Opening Eclipse CDT from the finder will not add /usr/local/bin to Eclipse’s Path! Opening Eclipse CDT from the command line will add /usr/local/bin to Eclipse’s Path. /usr/local/bin has to be part of the PATH as only then can Eclipse CDT execute ninja and cmake. This is summed up in the post: https://bugs.eclipse.org/bugs/show_bug.cgi?id=532419

You are now able to create a new C/C++ CMake project from within eclipse, clean it, build it and run the executable.

Python Cheatsheet

ToString() for Classes
class ObjModel(object):

    def __init__(self):
        self.vertices = []

    def __str__(self):
        return str(self.vertices[5][0])
Enumerations
class ObjFaceType(Enum):
    TRIANGLE = 1
    QUADRILATERAL = 2
    UNKNOWN = 999
Create Enumeration-Value from Integer
detectedFaceType = ObjFaceType(999)
Compare Value to Enumeration
if (currentFaceType == ObjFaceType.UNKNOWN):
Switch over Enumeration-Values

There is no such feature!

Create a new Class
# as a .obj file contains several objects, the ObjModelContainer stores all those objects
class ObjModelContainer(object):

    def __init__(self):
        self.objects = []
Create a Instance of a Class
objAdapter = ObjAdapter()