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

Re: asm printf?

Tue Jun 05, 2012 2:46 pm

Except computers (at assy level) think in twos rather than tens?
That's what I liked about the link I posted.

(Certainly made me think)

Dave

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

Re: asm printf?

Tue Jun 05, 2012 4:38 pm

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 :geek:
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?

Tue Jun 05, 2012 8:44 pm

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. :oops:

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

Re: asm printf?

Tue Jun 05, 2012 9:16 pm

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
ADD R1, R2,R3

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

Re: asm printf?

Tue Jun 05, 2012 10:07 pm

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)

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

Re: asm printf?

Tue Jun 05, 2012 10:28 pm

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?

Tue Jun 05, 2012 11:11 pm

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?

Thu Jun 07, 2012 8:57 am

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?

Mon Jun 11, 2012 9:50 am

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
3. sys_read
Syntax: ssize_t sys_read(unsigned int fd, char * buf, size_t count)
Source: fs/read_write.c
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

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

Re: asm printf?

Mon Jun 11, 2012 10:55 am

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?

Mon Jun 11, 2012 11:21 am

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?

Mon Jun 11, 2012 12:28 pm

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?

Wed Jun 26, 2013 3:18 pm

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?

Wed Jun 26, 2013 3:30 pm

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?

Thu Jun 27, 2013 3:24 pm

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

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

Re: asm printf?

Thu Jun 27, 2013 7:31 pm

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.

Return to “Advanced users”