Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
@Karlos

Code: [Select]
tst.l #d7
Should (obviously) be
Code: [Select]
tst.l d7
I also suggest not using spaces in asm:
Code: [Select]
moveq #1, d7 ; success!
but:
Code: [Select]
moveq #1,d7 ; success!
Some assemblers are known to fail with spaces in wrong places.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
There are several bugs in the code. Like pointed out by others, you must either move.l #label,reg or lea label,areg to get ptr to label.

Further your CON: string had a bug, it must be:
Code: [Select]
'CON:0/100/640/100/TesttextWindow',0
not
Code: [Select]
'CON:0/100/640/100 TesttextWindow',0

Your code printed '\0' char to console. This is not deadly, but causes confusion if you ever use the same method to stdout, and the output is redirected to file. Better learn to avoid it. Linefeeds were missing, too.

Also, your code was lacking all error checking, which I also added. Finally, your libopen LVO was really OldOpenLibrary, that is no longer supported. You must use OpenLibrary (-552).

Here's a working (tested) example:
Code: [Select]

; simple conwindow example

RETURN_OK         EQU 0
RETURN_FAIL       EQU 20
MODE_OLDFILE      EQU 1005
MODE_NEWFILE      EQU 1006

_LVOOpenLibrary   EQU -552
_LVOCloseLibrary  EQU -414

_LVOOpen          EQU -30
_LVOClose         EQU -36
_LVOWrite         EQU -48
_LVOIoErr         EQU -132
_LVODelay         EQU -198


main:
     moveq   #RETURN_FAIL,d7
     bsr     init
     tst.l   d0               ; worked?
     beq     exit

     lea     testtext1(pc),a0
     bsr     conputstr

     lea     testtext2(pc),a0
     bsr     conputstr

     moveq   #4,d0            ; wait 4 secs
     bsr     sleep

     bsr     cleanup

     moveq   #RETURN_OK,d7    ; all ok

exit:
     move.l  d7,d0
     rts

; in:  void
; out: d0.l success indicator, 0 for error, != 0 for success
init:
     movem.l d2/a6,-(sp)

     move.l  4,sysbase
     move.l  sysbase(pc),a6
     lea     dosname(pc),a1   ; open dos.library
     moveq   #33,d0
     jsr     _LVOOpenLibrary(a6)
     move.l  d0,dosbase
     beq     initerror

     move.l  dosbase(pc),a6   ; open console
     lea     consolename(pc),a0
     move.l  a0,d1
     move.l  #MODE_NEWFILE,d2
     jsr     _LVOOpen(a6)
     move.l  d0,conhandle
     beq     initerror

     moveq   #1,d0            ; success!
     bra     initpop

initerror:
     bsr     cleanup

     moveq   #0,d0            ; error!
initpop:
     movem.l (sp)+,d2/a6
     rts


; in:  void
; out: void
cleanup:
     move.l  a6,-(sp)

     move.l  dosbase(pc),d0   ; dos open?
     beq     nodosbase

     move.l  d0,a6
     move.l  conhandle(pc),d1 ; con handle open?
     beq     noconhandle
     jsr     _LVOClose(a6)
     clr.l   conhandle
noconhandle:

     move.l  sysbase(pc),a6
     move.l  dosbase(pc),a1
     jsr     _LVOCloseLibrary(a6)
     clr.l   dosbase

nodosbase:
     move.l  (sp)+,a6
     rts

; in:  a0.l = null terminating string to output
; out: void
conputstr:
     movem.l d2-d3/a6,-(sp)

     move.l  a0,d2
     moveq   #-1,d3
cps_strlen:
     addq.l  #1,d3
     tst.b   (a0)+
     bne     cps_strlen
     ; d3 str len, excluding terminating null

     move.l  dosbase(pc),a6
     move.l  conhandle(pc),d1
     jsr     _LVOWrite(a6)

     movem.l (sp)+,d2-d3/a6
     rts


; in:  d0.w = number of seconds to sleep
; out: void
sleep:
     move.l  a6,-(sp)

     move.l  dosbase,a6
     move.w  d0,d1
     mulu.w  #50,d1
     jsr     _LVODelay(a6)

     move.l  (sp)+,a6
     rts


dosname:     dc.b 'dos.library',0
consolename: dc.b 'CON:0/100/640/100/TesttextWindow',0

testtext1:   dc.b 'This thing works dude!!!',10,0

testtext2:   dc.b $9b,'4;31;40m'
             dc.b 'underline'
             dc.b $9b,'3;33;40m',$9b,'5;20H'
             dc.b '** Hey Dammit !! **',10,0

             CNOP 0,2
sysbase:     ds.l 1
dosbase:     ds.l 1
conhandle:   ds.l 1

 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
@RWO
Quote
this can be optimized...

It can be optimized. It can be also made totally obfuscated and useless for the guy trying to figure out basic 68k assembler.

Size optimization was not the focus here, but clear presentation, and trying to help the guy.

Quote
And if you want to live on the edge move dosbase into a5 and sysbase to a6. Normally library bases has to be in a6 but dos library dont care ;)

BEEP! Wrong.

That only was true for Kickstart 1.x (pre-V36). Kickstart V36+ dos.library requires library base in a6, like any other library.

PS. For size optimized 68k assembler, take a look at prim.ASM,  sarki.lzx or NanoCLS-src.lha.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Quote
Well I could sweare that it worked lasttime tryed in v39

It might work by luck for some functions, but you can't know for sure which ones. You cannot assume the functions will continue to work like this in future OS versions, either.

Anyway, this hack only works for Kickstart 1.x dos.library, because it uses moveq + bra in jumptable and direct bsr/bra to other dos.library functions inside dos.library (BCPL legacy stuff). This hacky code was removed in V36 dos.library, and dos.library was made normal library.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Quote
I'm pretty sure the old software that uses a5 for dos will still work

And I am sure they don't. Some functions don't need dosbase at all, but what I am 100% sure about is that dos.library functions do NOT look into A5 for dosbase, ever.

At least they will cause problems if dos.library functions are patched (the patch calling dos.library functions by itself & wrong library base in a6 -> boom).

Don't count on it. It won't work. Always put dosbase in a6 when calling dos.library.