Tuesday, 17 January 2012

Bounded ranges, constrained or clamped values

So, given the ability to build a jump table for a range of input, it would be useful to transform a given input value onto one of the valid options for that table. If you have a quick look at the pseudo code reproduced from Wikipedia, you'll see it has a validate x instruction, which if you ask me is an unapologetically brief way of describing a hashing, bounding, clamping or range-checking process. However, that's what this post is about: confining an input to a range of output values. It's actually unrelated to the generation or use of a jump table, but it doesn't take much imagination to see how it might be useful.

Switch Blocks & Jump Tables

I didn't know how jump tables were assembled prior to starting this investigation, and as anyone interested in assembly code will testify, using Google doesn't really help very much. Jonathan Bartlett's excellent "Programming From The Ground Up" and Appendix C, "C Idioms In Assembly Language" sadly doesn't cover switch blocks, but Wikipedia turns out to have a good entry.

Compare And Swap

Atomic instructions are used by the OS to provide higher-level concurrency constructs such as locks and semaphores. Probably the best known is the cmpxchg instruction, which takes two operands: a source register and destination register or address. To be useful in concurrent code, the destination operand will be a memory address. It is described in the Intel Software Developer's Manual Volume 2A at 3-188 (or page 260 according to my PDF reader) as follows:
Compares the value in the AL, AX, EAX, or RAX register with the [...] destination operand. If the two values are equal, the [...] source operand is loaded into the destination operand. Otherwise, the destination operand is loaded into the AL, AX, EAX or RAX register. RAX register is available only in 64-bit mode.

"In 64-bit mode, the instruction’s default operation size is 32 bits. [...] Use of the REX.W prefix promotes operation to 64 bits."

Anyway, here's an example.

The initial stack, reading process arguments (and environment variables)

I wrote a 32-bit assembly application a while ago which performed the simple task of printing out the program arguments and then the environment variables. Most people have seen a C-style main method:
int main(int argc, char* argv[]) {
 //...
}
Wikipedia tells me that "Unix (though not POSIX.1) and Windows have a third argument giving the program's environment", and takes the following form:
int main(int argc, char *argv[], char *envp[]) {
 //...
}
See libc_start_main.c at lines 58-67 for the mechanics of how the signature is varied between the two forms. Depending on which form is implemented the stack frame would look different as it would have a third argument pushed onto it. However, we're not going to rely on the glibc code to invoke a main method, we're going to implement a global function called _start and see what we're given: this is at a level below what you'd see in a C-style main function.

Syntax Highlighting for Assembly Code

Having put a fair amount of time into writing the posts I've published up so far, I've become disappointed with publishing code snippets in <pre> tags. There is Alex Gorbatchev's shiny JavaScript solution, but back when this blog was on Wordpress, I couldn't use it on as I couldn't supply my own 'brush' to format GNU assembler/gas code.

Calling Java from assembler

Hi there,

You may think that this is completely insane. We have C, right? You know, that high-level language which might still be popular come the end of the year?

Yes, but then that's hardly the point. I want to know how to do the same thing in assembly. So, with that in mind, here goes.

Monday, 16 January 2012

Calling ASM functions from Java

If you've been inquisitive enough to read the "About" pages you'll see that my day job involves writing software in Java. To that end, I've put together some code which demonstrates calling a function in a shared library (written in assembler). Hopefully the following will illustrate the steps involved fairly clearly.

Hello, World!

Well, here it is, the ubiquitous "Hello, World!" example.

Despite the title, there are a couple of interesting things to note in the code below. The first is calculating the length of the string hellotxt, the second being the different usage of the $ operator in GNU assembler.