Announcement

Collapse
No announcement yet.

Trivial Example request

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trivial Example request

    OK, mentioned elsewhere I like to learn by example. So, could some assembler guru demonstrate to the assembled (joke. Geddit?) newbs viewing, how the following (I'm particularly interested in the loops) would code out?

    (and I appreciate this is trivial and could gain any number of performance improvements by things such as backward loops etc. If that's all you see then you've missed the point.)

    Code:
    FUNCTION PBMAIN () AS LONG
        c&=0    
        FOR a&=1 TO 10
            FOR b&=a&+1 TO 11
                c&=c+a&+b&
            NEXT
        NEXT
        PRINT c&
        WAITKEY$
    END FUNCTION
    Neil Croft (cissp)

  • #2
    Neil,
    there are many ways to do it, here's one straight forward translation of your BASIC into ASM.

    Paul.
    Code:
    #REGISTER NONE                 'advise you stop compiler using register variales while you experiment with ASM, it can cause confusion
    
    FUNCTION PBMAIN () AS LONG
    LOCAL a&,b&,c&                 'variables used in ASM must be referenced somewhere before you use them
    
        'c&=0
        !mov c&,0                  'mov 0 to c&
        
        'FOR a&=1 TO 10
        !mov a&,1                  'move 1 to a&
        aloop:
            'FOR b&=a&+1 TO 11
            !mov eax,a&            'get a& into register. Can only have 1 memory reference in an instruction so need to go via a register, eax
            !add eax,1             'add 1 to value of a& in eax
            !mov b&,eax            'move result to b
            bloop:
    
                'c&=c&+a&+b&
                !mov eax,c&        'eax = c&
                !add eax,a&        'eax = c& + a&
                !add eax,b&        'eax = c& + a& + b&
                !mov c&,eax        'move result in eax to c&
    
            'NEXT
            !add b&,1              'add 1 to b&
            !cmp b&,11             'has it exceeded the loop limit?
            !jle bloop             'it's less than or equal so no, loop back.
    
        'NEXT
        !add a&,1                  'add 1 to a&
        !cmp a&,10                 'compare with loop limit
        !jle aloop                 'it's less than or equal so loop back
    
    
        PRINT c&                   'stick to BASIC for printing.
        WAITKEY$
        
    END FUNCTION
    Last edited by Paul Dixon; 12 Jul 2009, 12:45 PM.

    Comment


    • #3
      Top stuff. I particularly like the way that PB can be used to avoid all the nasty I/O stuff (disks and output etc.) and all the register storing stuff too.

      I found this http://www.arl.wustl.edu/~lockwood/c...ofasm/toc.html which gives a fairly broad view of assembler too so no doubt I shall be causing GPFs for the rest of the weekend whilst I mess around. :laugh:
      Neil Croft (cissp)

      Comment


      • #4
        Paul,

        Just a quick note to say I've played with your code and extended it to my needs on a couple of programs and I'm well impressed. Many thanks. Expect more questions soon and if you're ever in Micklebring then -> :beer:
        Neil Croft (cissp)

        Comment


        • #5
          Neil,
          if you're ever in Micklebring
          It's a bit far South for me, I don't often get beyond the North York Moors.

          Paul.

          Comment


          • #6
            Originally posted by Paul Dixon View Post
            Neil,

            It's a bit far South for me, I don't often get beyond the North York Moors.

            Paul.
            You haven't missed much. Far prettier up there.
            Neil Croft (cissp)

            Comment

            Working...
            X