## asm printf?

dpawson
Posts: 129
Joined: Mon Nov 14, 2011 5:05 pm
Contact: Website

### Re: asm printf?

Except computers (at assy level) think in twos rather than tens?

Dave

AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

### Re: asm printf?

At the risk of "doing your homework for you" rather than initialising k to 1 and using math.pow all the time, it'd be more efficient to initialise k to 10, and then do k *= 10 inside the loop
And your code breaks if you pass in a decimal number, which you might want to guard against.

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

### Re: asm printf?

Thanks Andrew - you make some good points!
And, yes, I know my program only works for multiplication of integers. It was one of the first programs I wrote in Python.

rurwin
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

### Re: asm printf?

AndrewS wrote: ... and then do k *= 10 inside the loop ...
The ARM has a multiply instruction, but the PDP-11 didn't have multiply either. So we had to do something like this:

Code: Select all

``````; Multiply R1 by 10
MOV  R2,R1,LSL#1
MOV R3,R2,LSL#2
``````

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

### Re: asm printf?

My first impression on reading your post (to the effect that the PDP11 did not have hardware multiply) is that my recollection is that it did. And, a brief web search backs my up on that.

As I recall, it acquired hardware multiply somewhere along the way. It could well be that the PDP11/45 did not, but the PDP11/70 did.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

rurwin
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

### Re: asm printf?

We were writing code for an 11/40, with an 11/25 doing the operating system stuff.
Wikipedia wrote:[MUL and DIV] was an option for 11/35/40 and 11/03, and was standard on newer processors.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

### Re: asm printf?

OK, then we agree. I remember being very happy to get it when we moved to the 70.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

### Re: asm printf?

I am very grateful for the comments of folks on here.
As I don't consider myself a power user, I've posted a bit of code I wrote to do the first algorithim in Knuth's TAOCP here.

It's in asm (with a little bit of "printf" !).
mark

dpawson
Posts: 129
Joined: Mon Nov 14, 2011 5:05 pm
Contact: Website

### Re: asm printf?

Taking this a little further.
http://asm.sourceforge.net/syscall.html shows the kernel etc calls, which relate cleanly to
the definitions at
http://lxr.free-electrons.com/source/ar ... =3.1;a=arm
E.g
Syntax: ssize_t sys_read(unsigned int fd, char * buf, size_t count)
Action: read from a file descriptor
So it would be
r0 1 (stdin = 1????)
r1 buf*
r2 returned count
r7 3 (svc 3)

The fog begins to clear.

Thanks to all

Dave
I might guess that return values (buffer ptr in this case?) are in the buffer pointed to

rurwin
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

### Re: asm printf?

dpawson wrote: r0 1 (stdin = 1????)
No, r0 should be zero.

See this for enlightenment.
dpawson wrote: r2 returned count
That's wrong too. R2 holds the size of the buffer going into the call. The number of characters read is the return value from the call. R0?

dpawson
Posts: 129
Joined: Mon Nov 14, 2011 5:05 pm
Contact: Website

### Re: asm printf?

Thanks rurwin.

input:
space 96,0

ldr r1,=input
mov r2, #96
mov r0, 0
mov r7, #3
svc #0

ldr r1, =input
mov r2, r0
bl print @ subr to print

Works nicely.

Dave

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

### Re: asm printf?

This is excellent! I shall try the code myself later!
mark

Loki
Posts: 11
Joined: Thu Jun 20, 2013 2:37 pm

### Re: asm printf?

hi all,
does anyone know where exactly the svc / swi command is handled? i'm running raspbian. could i use that handler to perform assembly code which needs a privileged processor mode?
best regards
loki

tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

### Re: asm printf?

https://github.com/raspberrypi/linux/bl ... el/traps.c line 538 or so

You'll need to patch into arm_syscall (and possibly a few other places). That should let you set up your own ad-hoc hander in the syscall interface. Remember, though, you're programming in kernel context, you could bring your whole system down around your knees.

You could attempt to make it an "official" syscall, which would be marginally faster, but more dangerous.

Alternatively, and rather more hairily, you could pry patching directly into https://github.com/raspberrypi/linux/bl ... y-common.S which contains the actual swi/syscall hander.

Loki
Posts: 11
Joined: Thu Jun 20, 2013 2:37 pm

### Re: asm printf?

wow, didn't expect such a fast answer in such a dated post. really nice, that was exactly what i was looking for. i think i'm going to try the hairy method but i have no time right now. will maybe try it in a week or two and then return because nothing will work anymore =).

thanks a real lot tufty!

best regards
loki

rurwin
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

### Re: asm printf?

At the expense of a little less efficiency, the Linux way to do this is to write a device driver. If you search the forum for "servoblaster", you might get some good hints how to go about that.