简体   繁体   中英

68K Assembly: Search number in numbers file

I am using Easy68k to write an assembly program, where I have to write a script for searching a number inside a file with numbers.

File numbers4.txt :

1
2
3
4
5
6
7
9
11
12
13
14

My Code :

    ORG    $1000

START:                  ; first instruction of program

*------------File Handling ------------*

        lea         filename, a1
        move        #51, d0
        trap        #15

        move.l      #filesize, d2
        lea         buffer, a1
        move        #53, d0
        trap        #15 

*------------Save Address in A1------------*

        movea.l     a1, a3      ; pointer for file values



*------------Searching Value Loop------------*  

            clr.l   d3          ; value at index
search      move.b  (a3)+,d3
            *-- Compare Here --*
next        cmpi.b  #$FF,d3
            bne.s   search



* Put program code here


            SIMHALT             ; halt simulator

* Put variables and constants here
    org    $2000

filename    dc.b    'numbers4.txt',0
buffer      ds.w    80
filesize    dc.b    80


    END    START        ; last line of source

The file values loaded to the memory :

在此处输入图片说明

I am stuck at the part where I have to compare values. I know how to compare single digit values 0-9 (ie : subtract 30) to hex but how do the compare double digit or beyond with hex? like how to a check if hex "0B" is one of the ascii values (31 31) that is in the memory. Or Perhaps my approach is incorrect I am not sure.

I am a newbie so my apologies if I am missing something obvious. Please help

I would create a sub-routine to read the numbers into memory/register. Process each digit in turn until you hit the end of the line (Carriage return etc). Essentially process the text into a true integer value to store in memory. Then the values will be simple to work with.

When reading in a number load it a character at a time into a data register, if the next character is also a number then multiply what you have in the data register by 10 and add the new number. If the source numbers were in Hex this would be nicer as you could simply shift the data register left 4 bits and or in the new digit.

If the next char isn't a number, the routine should then return.

So simple loop:

loadval: clr.l   d0        ; result in d0
         clr.l   d1        ; working reg
.loop:   move.b  (a3)+, d1
         cmpi.b  #$30, d1  ; check for numerical digit
         blt     .done
         cmpi.b  #$39, d1
         bgt     .done

         sub.b   #$30, d1  ; We have a number, add it 
         mult.w  #10, d0
         add.w   d1, d0
         bra.s   .loop
.done:   rts

Hopefully that makes sense and works (just scribbled it down off the top of my head so may be a bit buggy :D )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM