Welcome, Guest. Please login or register.

Author Topic: Arghhh What is wrong with this asm code? (must be something stupid...)  (Read 4601 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
@Jose

I don't mean to pee on your parade, but this is not the place to use asm - unless you are aiming for very small exe size, at least. This kind of thing is far easier and much less error prone in C.

Mixing the two (C and asm) isn't as tricky as you might think ;-)

Souldn't your init code return after a branch to error?

error:
rts

In an error occurs, your main function never returns because of this.

You should put some sort of return code there too, so the caller can tell an error occured and skip the call to write.

So, the end of your init looks like this

...

move #1, X ; success!
rts

error:
move #0, X ; tittybiscuits!
rts

where X is some register or variable that you check afterwards.

You need to test the result in X after calling init to see if it was successful or not, eg tst.l dN followed by a suitable conditional branch to an exit or something.

So suppose we use d7 for X (for sake of argument)

Your complete code would then look like
Code: [Select]
[size=x-small]
execbase =4
openlib =-408
closelib =-414

open =-30
close =-36
write =-48
IoErr =-132
mode_old =1005
alloc_abs =-$cc

main:
     bsr init ; returns zero in d7 if error
     tst.l d7
     beq exit

     move.l conhandle,d1
     move.l testtext1,d2
     move.l #testtextend-testtext1,d3
     move.l dosbase,a6
     jsr write(a6)

exit:
     ; todo insert cleanup code
     rts

init:
     move.l execbase,a6 ;open dos.library
     lea dosname(pc),a1
     moveq #0,d0
     jsr openlib(a6)
     move.l d0,dosbase
     beq error

     move.l dosbase,a6    ;open console
     move.l #mode_old,d2
     move.l consolename,d1
     jsr open(a6)
     move.l d0,conhandle
     beq error

     moveq #1,d7 ; success!
     rts

  error:
     moveq #0,d7 ; tittybiscuits
     rts

dosname dc.b "dos.library",0,0
consolename dc.b 'con:0/100/640/100 TesttextWindow',0
            align 0,2

dosbase ds.l 1
conhandle ds.l 1

testtext1 dc.b 'This thing works dude!!!'
testtext2 dc.b $9b,'4;31;40m'
 dc.b 'underline'
 dc.b $9b,'3;33;40m',$9b,'5;20H'
 dc.b '** Hey Dammit !! **',0
testtextend
[/size]


[/quote]
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
@Thomas

move.l consolename, d1

:lol: How did I manage to overlook that?

(Spot the guy who only converts functions to asm after all else failed to speed them up)
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
@Piru

Aye. I think the # was left in there accidentally as I was copying and pasting wihtout much thinking :-)

Man, I sure know how to make myself look stupid in public :-D
int p; // A