Tuesday, 25 March 2014

Assembly Syntax: Intel & AT&T

This post is just a little cheat sheet for myself on Intel & AT&T syntax.

A useful table mapping some simple instructions between the two syntaxes linked through from the GCC-Inline-Assembly-HOWTO:

|       Intel Code             |      AT&T Code                     |
| mov     eax,1                |  movl    $1,%eax                   |   
| mov     ebx,0ffh             |  movl    $0xff,%ebx                |   
| int     80h                  |  int     $0x80                     |   
| mov     ebx, eax             |  movl    %eax, %ebx                |
| mov     eax,[ecx]            |  movl    (%ecx),%eax               |
| mov     eax,[ebx+3]          |  movl    3(%ebx),%eax              | 
| mov     eax,[ebx+20h]        |  movl    0x20(%ebx),%eax           |
| add     eax,[ebx+ecx*2h]     |  addl    (%ebx,%ecx,0x2),%eax      |
| lea     eax,[ebx+ecx]        |  leal    (%ebx,%ecx),%eax          |
| sub     eax,[ebx+ecx*4h-20h] |  subl    -0x20(%ebx,%ecx,0x4),%eax |

Some important points to note:

  • Source and destinations are flipped in opcodes.
    • Intel is dest, src
    • AT&T is src, dest
  • AT&T decorates registers and immediates
    • Registers are prefixed with a "%"
    • Immediates are prefixed with a "$". This applies to variables being passed in from C (when you're inline).
  • Intel decorates memory operands to denote the operand's size, AT&T uses different mnemonics to accomplish the same.
  • Intel syntax to dereference a memory location is "[ ]". AT&T uses "( )".

Monday, 24 March 2014

xargs -i

This post is a just a tid-bit for the use of xargs in bash.

If you can get a list of work to do from a file or stream, you can pipe these into xargs to do further work. An example of this, I've taken from here. This will find all of the *.bak in or below the current directory and delete them.

$ find . -name "*.bak" -type f -print | xargs /bin/rm -f

Extending the usage of xargs to incorporate the -i switch, you can replace "{}" with the line of text read in the preprended command.

$ cat url_endings | xargs -i wget "http://somewhere.com"

In this command, it's expected that the file "url_endings" would be data that looks like this:

etc . . 

Wednesday, 19 March 2014

A Quick Lap with MVar


Concurrent programming is hard. It's made a lot easier with good tools and MVar is one of them. MVars is just a location for a value. It can contain a value or contain nothing and the API will block accordingly, providing a safe concurrent programming environment for mutable state.

From the Hackage page for Control.Concurrent.MVar:
An MVar t is mutable location that is either empty or contains a value of type t. It has two fundamental operations: putMVar which fills an MVar if it is empty and blocks otherwise, and takeMVar which empties an MVar if it is full and blocks otherwise.

Key Points

  • newEmptyMVar creates an MVar that has no value to begin with
  • newMVar creates an MVar that has an initial value
  • takeMVar returns the current value of the MVar. It'll block until the MVar contains a value
  • putMVar puts a value into the MVar. It'll block until the MVar doesn't contain a value

An Example

A Quick Lap with Lens


When working with complex data structures in Haskell, burying down to observe a piece of information can be tedious. The Lens library has been created to ease this problem. From the Lens wiki page:
Lenses are composable functional references. They allow you to access and modify data potentially very deep within a structure!
The Lens library allows you to interact with your data structures in a composable manner, making your code easier to understand - and more fun to write.

Key Points

  • Whilst there are a lot of different functions, (^.) allows you to get some data and (.~) allows you to set some data
  • makeLenses is what does all the magic of creating your accessors
  • I haven't found anywhere that specifically says this, but it seems that your fields in a record structure need to be preceded with an underscore

An Example

Sunday, 16 March 2014

cabal sandbox ghci

A very quick post that is based on the information from this stack overflow article on how to get a GHCi session up and running with the libraries referenced that you've installed using your cabal sandbox.


# For GHC >= 7.6
$ ghci -no-user-package-db -package-db .cabal-sandbox/i386-linux-ghc-7.6.1-packages.conf.d

# For GHC < 7.6
$ ghci -no-user-package-conf -package-conf .cabal-sandbox/i386-linux-ghc-7.4.2-packages.conf.d

A Quick Lap with the State Monad


The State monad gives functionality of both the Reader monad and Writer monad in one. When using the State monad you're able to read the state at any time and then set it back again, providing read/write access.

Key Points

  • The function get is used to read the current state
  • The function put is used to set the state
  • runState is used to manage execution of functions that run in the State monad
  • Operations in the State monad can use >>= to be chained together
  • Functions in the State monad are decorated with State s v. Where "s" is the type of the state and "v" is the return type from the function

An Example

A Quick Lap with the Writer Monad


The Writer monad allows functions to accumulate information as functions execute. According to the Hackage page:
A writer monad parameterized by the type w of output to accumulate.
Perhaps not the most verbose of descriptions, however this is rather simple to explain with a well known example. In previous programming disciplines you would have needed to log information out of your code as your program "did things". The Writer monad allows you to write out information in a log form. This doesn't necessarily have to be in textual log format; an example I have seen recently is to keep track of calculations used to come to a final result. The calculations put into that log sped up calculations on other figures.

The idea here is to not clutter your code having to support things like logging/tracing, etc. Employing this monad gives your code the ability to produce this output on the side without getting in the way.

Key Pieces

  • Functions in the Writer monad are decorated with Writer l r. "l" in this case is the type that you'll be logging out where "r" is the result being returned from your function.
  • The function tell is what's used to push another value into the log/trace/writer.
  • Operations in the Writer monad can be chained together using >>=.
  • runWriter is what you'll use to run something in the Writer monad to get your result back.

An Example