Category Archives: Tech

ARM assembly, take 3

After having considered some comments from Mr./Ms. G I optimized my assembly code a little bit.
Here a better approach:

Output
deadbeef

11011110101011011011111011101111

00000000111111111111111111111111

Code:

.equ STRING_LENGTH, 32
.equ ZERO_CHARACTER, 0x30
.equ ONE_CHARACTER, 0x31

.data
Mask:
	.ascii  "%x\n\n"
.align 4 

varToPrint:
	.long 0xdeadbeef
.balign 8 

varToUnspace:
	.long 0xdeadbeef
.balign 8 

unspacedVarToPrint:
	.long 0xdeadbeef
.balign 8 

stringToPrint:
	.ascii "                                                                                  \n\n"
len = STRING_LENGTH
.balign STRING_LENGTH

unspacedStringToPrint:
	.ascii "                                                                                  \n\n"
len = STRING_LENGTH
.balign STRING_LENGTH

.text
.globl main
main:
	stmfd	sp!, {r0, r1, r2, r3, r4, r5, r6, r7, lr}	@ save the registers we use to the stack

	ldr	r3, AddrVarToPrint
	ldr	r6, [r3]
	ldr	r3, AddrVarToPrint
	str	r6, [r3]

	/*
	Generating string of binary representation:
		Address of variable in r0
		Address of target string in r1
	*/
	ldr	r0, AddrVarToPrint
	ldr	r1, AddrStringToPrint
	bl make_binary_number
	/*------------------------------------------*/

	/*
	Removing zeroes between the ones in binary value:
		Address of variable in r0
		Address of target variable in r1
	*/
	ldr	r0, AddrVarToPrint
	ldr	r1, AddrUnspacedVarToPrint
	bl remove_spaces

	/*
	Generating string of binary representation:
		Address of variable in r0
		Address of target string in r1
	*/
	ldr	r0, AddrUnspacedVarToPrint
	ldr	r1, AddrUnspacedStringToPrint
	bl make_binary_number
	/*------------------------------------------*/

	bl print_out_result

	b exit

/*-Fetching the values and printing them-----------------------*/
print_out_result:
	push	{r0, r1, r2, r3, r4, lr}

	ldr     r3, AddrMask
	movs	r0, r3
	ldr	r3, AddrVarToPrint
	ldr	r1, [r3]
	bl	printf

	ldr     r3, AddrStringToPrint 
	movs	r0, r3
	bl	printf

	ldr     r3, AddrUnspacedStringToPrint
	movs	r0, r3
	bl	printf

	pop	{r0, r1, r2, r3, r4, pc}

/*-Generate a binary representation string from integer--------*/
/* Address of variable in r0 */
/* Address of target string in r1 */
make_binary_number:
	push	{r2, r3, r4, r5, r6, r7, lr}
	ldr	r3, [r0] /* integer value of variable */

	movs	r4, $0 /* loop counter */
bin_loop:
	movs    r2, $0x1
	lsls	r2, r4 /* r2 = (0x01 >> counter) */

	movs	r6, $STRING_LENGTH-1
	subs	r6, r6, r4 /* idx = reg_length-counter */

	tst	r3, r2
	beq	no_one

	movs	r5, $ONE_CHARACTER
	strb	r5, [r1,r6]
	b end_bin_loop

no_one:
	movs	r5, $ZERO_CHARACTER
	strb	r5, [r1,r6]
	b end_bin_loop

end_bin_loop:
	adds	r4, r4,$1
	cmp	r4, $STRING_LENGTH
	bne	bin_loop

	pop	{r2, r3, r4, r5, r6, r7, pc}

/*-Removes spaces between ones---------------------------------*/
/* Taken we have: 0xdeadbeef
	Binary representation: 11011110101011011011111011101111
	Then the ones will be shifted together so that we get
	the following result: 00000000111111111111111111111111
*/
/* Address of variable in r0 */
/* Address of target variable in r1 */
remove_spaces:
	push	{r2, r3, r4, r5, r6, r7, lr}
	ldr	r6, [r0]

	movs	r4, $0
	movs	r5, $0
	movs	r7, $0

unspace_loop:
	movs    r2, $0x1

	lsls	r2, r4

	tst	r6, r2
	beq	end_unspace_loop

	lsls	r5, r5, $1
	adds	r5, r5, $1

end_unspace_loop:
	adds	r4, r4,$1
	cmp	r4, $STRING_LENGTH
	bne	unspace_loop

	str	r5, [r1]

	pop	{r2, r3, r4, r5, r6, r7, pc}
/*-------------------------------------------------------------*/

/*--------EXIT main(void) function -> return 0-----------------*/
exit:
	ldmfd	sp!, {r0, r1, r2, r3, r4, r5, r6, r7, pc}	@ restore registers before exit
	movs	r7, $1                  @ set r7 to 1 - the syscall for exit
	swi	0                       @ then invoke the syscall from linux


/*-----Variable pointers---------------------------------------*/
AddrMask:
	.word	Mask

AddrVarToPrint:
	.long	varToPrint

AddrVarToUnspace:
	.long	varToUnspace

AddrStringToPrint:
	.word stringToPrint

AddrUnspacedStringToPrint:
	.word unspacedStringToPrint

AddrUnspacedVarToPrint:
	.long unspacedVarToPrint

ARM assembly

Eliminating zeroes between ones from binary number:
Output
deadbeef

11011110101011011011111011101111

111111111111111111111111

Code

.data
Mask:
	.ascii  "%x\n\n"
.align 4 

varToPrint:
	.long 0xdeadbeef
.balign 8 

stringToPrint:
	.ascii "                                                                                  \n\n"
len = 32
.balign 32

unspacedStringToPrint:
	.ascii "                                                                                  \n\n"
len = 32
.balign 32

.text
.globl main
main:
	stmfd	sp!, {r0, r1, r2, r3, r4, r5, r6, r7, lr}	@ save the registers we use to the stack

	ldr	r3, AddrVarToPrint
	ldr	r6, [r3]
	ldr	r3, AddrVarToPrint
	str	r6, [r3]

	bl make_binary_number

	bl remove_spaces

	bl print_out_result

	b exit

print_out_result:
	push	{r0, r1, r2, r3, r4, lr}

	ldr     r3, AddrMask
	movs	r0, r3
	ldr	r3, AddrVarToPrint
	ldr	r1, [r3]
	bl	printf

	ldr     r3, AddrStringToPrint 
	movs	r0, r3
	bl	printf

	ldr     r3, AddrUnspacedStringToPrint
	movs	r0, r3
	bl	printf

	pop	{r0, r1, r2, r3, r4, pc}

make_binary_number:
	push	{r0, r1, r2, r3, r4, r5, r6, r7, lr}
	ldr	r3, AddrVarToPrint
	ldr	r1, [r3]

	movs	r4, $0
bin_loop:
	movs    r2, $0x1

	lsls	r2, r4
	movs	r6, $31
	subs	r6, r6, r4

	tst	r1, r2
	beq	no_one

	ldr	r3, AddrStringToPrint
	movs	r5, $0x31
	strb	r5, [r3,r6]
	b end_bin_loop

no_one:
	ldr	r3, AddrStringToPrint
	movs	r5, $0x30
	strb	r5, [r3,r6]
	b end_bin_loop

end_bin_loop:
	adds	r4, r4,$1
	cmp	r4, $32
	bne	bin_loop

	pop	{r0, r1, r2, r3, r4, r5, r6, r7, pc}

remove_spaces:
	push	{r0, r1, r2, r3, r4, r5, r6, r7, lr}
	ldr	r3, AddrVarToPrint
	ldr	r1, [r3]

	ldr	r3, AddrUnspacedStringToPrint
	movs	r5, $0x31

	movs	r4, $0
	movs	r7, $0

unspace_loop:
	movs    r2, $0x1

	lsls	r2, r4

	tst	r1, r2
	beq	end_unspace_loop

	strb	r5, [r3,r7]

	adds	r7,r7,$1

end_unspace_loop:
	adds	r4, r4,$1
	cmp	r4, $32
	bne	unspace_loop

	pop	{r0, r1, r2, r3, r4, r5, r6, r7, pc}

exit:
	ldmfd	sp!, {r0, r1, r2, r3, r4, r5, r6, r7, pc}	@ restore registers before exit
	movs	r7, $1                  @ set r7 to 1 - the syscall for exit
	swi	0                       @ then invoke the syscall from linux

AddrMask:
	.word	Mask

AddrVarToPrint:
	.long	varToPrint

AddrStringToPrint:
	.word stringToPrint

AddrUnspacedStringToPrint:
	.word unspacedStringToPrint

printfs syscall in ARM assembly

You can assemble and link it with

arm-linux-gnueabi-gcc --static hello.S -o hello
.data
LC0:
	.ascii  "%d\n"
.text
.align  2

.globl main
main:
	stmfd	sp!, {r0, r1, r2, r3, r4, r5, r6, lr}	@ save the registers we use to the stack
	movs	r4, $0
loop:
	ldr     r3, L3
	movs	r0, r3
	movs	r1, r4
	bl	printf

	adds	r4, r4,$1
	cmp	r4, $100
	bne	loop

exit:
	ldmfd	sp!, {r0, r1, r2, r3, r4, r5, r6, pc}	@ restore registers before exit
	movs	r7, $1                  @ set r7 to 1 - the syscall for exit
	swi	0                       @ then invoke the syscall from linux

L3:
	.word	LC0
	.size	main, .-main

geekdarling.ch – Matching algorithm

Hey folks
Since the GPLed dating platform code out there is crappy PHP work and not suited to help geeks meet each other either, I started designing a new matching algorithm in course of the rewrite of geekdarling.ch as a Ruby application.
Basically the idea is quite easy. First I travel the bipartite tree and cut it into subtrees matching the basic requirement of matching sexual orientation and this way saving CPU power by checking for bidirectional preferences.

Second step: I will then value additional attributes based on a two way connection and average the weight of every forth and back arrow.

For the example on the upper graphics that would mean: R=(((6+1)/2)+((3+4)/2)+0)/3=2.3
Illuminated! 😉

Another practical example: Let’s say I choose under “preferred editor” select that it’s absolutely important to me that my partner like vim too, then anyone who likes vim as well will be given 100 additional points when being weighted in our matching algorithm, and so all the vim using geek girls are listed first (followed by the emacs users ;-)).

I’m not quite done yet, but that’s where I’m going 🙂
Please gimme feedback.

sending HTML emails with inline images attached

#! /usr/bin/python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header

def sendEmail(you,greatingLine):
        # me == my email address
        # you == recipient's email address
        me = "VFSN Praesident "

        # Create message container - the correct MIME type is multipart/alternative.
        #msg = MIMEMultipart('alternative')
        msg = MIMEMultipart('related')
        msg['Subject'] = Header("Verein Flugschneise Süd - NEIN","utf-8")
        msg['From'] = me
        msg['To'] = you

        # Create the body of the message (a plain-text and an HTML version).
        f = open('Mail_4.html', 'r')
        mail_content = f.read()
        f.close()
        mail_content = mail_content.replace('%Anrede%',greatingLine)
        mail_content = mail_content.replace('vfsn.gif','cid:vfsn.gif')
        html = mail_content
                                                                                                                                                                                                
        # Record the MIME types of both parts - text/plain and text/html.                                                                                                                       
        part2 = MIMEText(html, 'html')                                                                                                                                                          
                                                                                                                                                                                                
        part3_data = open('vfsn.gif', 'rb').read()                                                                                                                                              
        part3 = MIMEImage(part3_data, 'gif')                                                                                                                                                    
        part3.add_header('Content-ID', '')                                                                                                                                            
        part3.add_header('Content-Disposition', 'attachment', filename='vfsn.gif')
        part3.add_header('Content-Disposition', 'inline', filename='vfsn.gif')

        part4_data = open('Bauverbot im Sueden_2_LQ.pdf', 'rb').read()
        part4 = MIMEImage(part4_data, 'pdf')
        part4.add_header('Content-Disposition', 'attachment', filename='Bauverbot im Sueden_2_LQ.pdf')
        part4.add_header('Content-Disposition', 'inline', filename='Bauverbot im Sueden_2_LQ.pdf')

        # Attach parts into message container.
        # According to RFC 2046, the last part of a multipart message, in this case
        # the HTML message, is best and preferred.
        msg.attach(part2)
        msg.attach(part3)
        msg.attach(part4)

        # Send the message via local SMTP server.
        s = smtplib.SMTP('localhost')
        # sendmail function takes 3 arguments: sender's address, recipient's address
        # and message to send - here it is sent as one string.
        s.sendmail(me, you, msg.as_string())
        s.quit()

csv = open('Adressen_4.csv','r').read()
for line in csv.split('\n')[1:]:
        lineArray=line.split(',')
        try:
                ret=lineArray[7]+" "+lineArray[4]+" "+lineArray[5]
                ret=ret.strip()
                if ret=="":
                        ret=False
        except:
                ret=False

        Anrede=ret

        try:
                ret=lineArray[12]
                ret=ret.strip()
                if ret=="":
                        ret=False
        except:
                ret=False

        Mail = ret

        if Mail and Anrede:
                print 'Sending to '+Mail+' with rcp message '+Anrede
                try:
                        sendEmail(Mail,Anrede)
                except:
                        continue

openerp on CentOS-6 (with RPM)

Howdy
First fit the package depencies like described here: http://gurn3ys.tuxfamily.org/?p=65
Somehow the installation prefix of the RPMs from the daily build service(http://nightly.openerp.com/7.0/nightly/rpm/) points to /usr/local which is… unhandy
The solution is to overwrite some rpm macros which check for unpacked files…
echo “%__os_install_post %{nil}” >> ~/.rpmmacros
And then installing and rebuilding the srpm with rpmbuild -ba

networkboot, u-boot, tftp

Hi
I’m using the following boot script in order to boot uImage and device tree blob directly from my PC.
No SD card removal, no flashing.
It boots out of RAM immediately
Edit ips and ethernet address accordingly to your network and hardware configuration.
Enjoy

setenv initrd_high 0xffffffff                                                      
setenv fdt_high 0xffffffff
setenv bootargs "root=/dev/mmcblk0p1  rootdelay=10 rw console=ttyS0,115200 panic=10 clk_ignore_unused"
setenv ethaddr D2:91:80:B7:AB:5C
setenv serverip 192.168.1.147
setenv ipaddr 192.168.1.145
ext4load mmc 0 0x43000000 boot/script.bin
tftp 0x49000000 sun7i-a20-cubieboard2.dtb
tftp 0x46000000 uImage
bootm 0x46000000 - 0x49000000