Author Topic: Struggling with ASM  (Read 4651 times)

0 Members and 1 Guest are viewing this topic.

Offline Eiyeron

  • Urist McEiyolobster
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1430
  • Rating: +130/-10
  • (-_(//));
    • View Profile
    • Rétro-Actif : Rétro/Prog/Blog
Struggling with ASM
« on: March 09, 2014, 07:48:29 am »
HI guys, I'm trying to learn ASM and I have a little problem : It I get a TLB eror when  the program seems to access to LTORG content.


I'm using Macro AS because I'm getting fed with FX SDK, and here is the source code I did :
Code: [Select]

   CPU SH7700
   LISTING ON
   SEGMENT code


;Syscall macro
syscall   macro   op
   MOV.L    op, R0;
   MOV.L    scall_table, R2;
   JMP @R2
   NOP
   endm


main:
   ;STS.L       PR,@-R15; I got this idea when reading FX SDK listings.
   MOV.W    #5, R4; Trying to give as argument id = 5 to DisplayErrorMessage
   syscall scall_DislayErrorMessage; Should print an error message
   NOP
   ; Stop? I don't really know how to stop that.
   ;LDS.L       @R15+,PR
   MOV      #1, R0
   RTS
   NOP; By here it should have stopped
   LTORG; But he goes on further


;Data
; It doens't accept SEGMENT DATA
String                  dc.B "Hello World!\0"; And crashes here accroding to the listing
   ALIGN 4
;syscalls
scall_Print_OS            dc.L $013c
   ALIGN 4
scall_DislayErrorMessage   dc.L $0954
   ALIGN 4
scall_table               dc.L $80010070


WIth tha code, I have a TLB error with TARGET 0x12 (?) and PC = 0x1E (in the Hello world string Oo). Why?


Addendum : I chose to not use FX SDK because I don't know how to include properly ASM. Do you have any good idea/methods?
« Last Edit: March 09, 2014, 07:50:53 am by Eiyeron »

Offline TeamFX

  • LV3 Member (Next: 100)
  • ***
  • Posts: 49
  • Rating: +9/-0
    • View Profile
Re: Struggling with ASM
« Reply #1 on: March 09, 2014, 12:55:03 pm »
Quote
I chose to not use FX SDK because I don't know how to include properly ASM. Do you have any good idea/methods?

You can include any asm function in a C program when you export it with a function name _FuncName

Here is a small asm-only program:

Code: [Select]
        .INCLUDE "syscall.inc"

        .MACRO SC SysCall
        mov.l   #\SysCall, r0
        mov.l   #JumpTableTop, r2
        jsr     @r2
        nop
        .ENDM

        .EXPORT _BR_Size
        .EXPORT _AddIn_main
        .SECTION P, CODE, ALIGN=4

_AddIn_main:
        SC      Bdisp_AllClr_VRAM
        mov     #5, r4                  ; col
        mov     #3, r5                  ; row
        SC      locate
        mov.l   #text, r4               ; str
        mov     #1, r5                  ; mode
        SC      Print

loop:
        mov.l   #key, r4                ; key
        SC      GetKey
        bra     loop
        nop

        rts
        nop

        .SECTION C, DATA, ALIGN=4
text        .SDATAZ "Hello, world!"

        .SECTION B, DATA, ALIGN=4
_BR_Size    .RES.L  1
key         .RES.L  1

        .END

Code: [Select]
JumpTableTop            .EQU    h'80010070

Print                   .EQU    h'013C
Bdisp_AllClr_VRAM       .EQU    h'0143
locate                  .EQU    h'0807
GetKey                  .EQU    h'090F

« Last Edit: March 09, 2014, 02:39:36 pm by TeamFX »

Offline Eiyeron

  • Urist McEiyolobster
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1430
  • Rating: +130/-10
  • (-_(//));
    • View Profile
    • Rétro-Actif : Rétro/Prog/Blog
Re: Struggling with ASM
« Reply #2 on: March 09, 2014, 01:02:30 pm »
Ok, how can I call C functions from an assembler file? .INCLUDE "header.h" won't work because syntax is way too different.

Offline TeamFX

  • LV3 Member (Next: 100)
  • ***
  • Posts: 49
  • Rating: +9/-0
    • View Profile
Re: Struggling with ASM
« Reply #3 on: March 09, 2014, 01:15:11 pm »
.IMPORT _FuncName should do it.

Refer to section "9.3.2 Function Calling Interface" in the SHC manual.
The SDK manuals are available as a separate download at edu.casio.com

Offline SpiroH

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +153/-23
    • View Profile
Re: Struggling with ASM
« Reply #4 on: March 10, 2014, 10:43:13 am »
Ok, how can I call C functions from an assembler file? .INCLUDE "header.h" won't work because syntax is way too different.
Well, you'll need to understand the calling (parameter passing) convention for that compiler. Then it should be straightforward.  More, see wikipedia: http://en.wikipedia.org/wiki/Calling_convention

Offline Siapran

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +28/-1
  • Space Dwarf
    • View Profile
Re: Struggling with ASM
« Reply #5 on: March 10, 2014, 03:34:09 pm »
so, according to wikipedia:
R0..R3 are temporary variables
R4..R7 are arguments
R8..R14 are variables that must be preserved
R15 is the stack pointer

SH3's assembly looks a lot like M68K's
however, the parameter passing conventions aren't the same
page 200 of the Renesas compiler documentation gives an example of the structure of a function and a function call.
« Last Edit: March 10, 2014, 03:40:34 pm by Siapran »

Offline Eiyeron

  • Urist McEiyolobster
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1430
  • Rating: +130/-10
  • (-_(//));
    • View Profile
    • Rétro-Actif : Rétro/Prog/Blog
Re: Struggling with ASM
« Reply #6 on: March 10, 2014, 04:23:56 pm »
Must be preserved, or are automatically preserved by the processor already? I think I'm getting the handle, except the fact I get a warning about an illegal displacement here
Code: [Select]
MOV.L @(2,R15), R4and wrong mnemonic with using a macro
Code: [Select]
   .MACRO FIX Rd
   SHLR8 \Rd
   SHLR4 \Rd
   SHLL \Rd
   .ENDM

...
   FIX   R10            ; FIX(pz) -> pz // And here goes the mnemonic error



And yipee, gotta read a cr*pload of books! yay :|

Offline Siapran

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +28/-1
  • Space Dwarf
    • View Profile
Re: Struggling with ASM
« Reply #7 on: March 11, 2014, 09:55:01 am »
they must be preserved, which means that the context before a function call and after a function call must have the same values for these registers.
so basically you have to save them to the stack when you use them in a function.

Offline Eiyeron

  • Urist McEiyolobster
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1430
  • Rating: +130/-10
  • (-_(//));
    • View Profile
    • Rétro-Actif : Rétro/Prog/Blog
Re: Struggling with ASM
« Reply #8 on: March 12, 2014, 05:14:10 am »
Yeah, I thought so. Yeah, refactor.