1

I am given this assignment:

This program is supposed to search an array of bytes for the value 0xf2. When it finds 0xf2, it should save its location (i.e. address) into the integer variable “f2Address”. If it does not find the value of 0xf2 in the array, it should put a value of 0x00 into the variable “f2Address”. This program has some bugs in it. Fix this program so that it does work properly.

The original code given was:

.label   TABLE1_LOC_START
TABLE1  .byte   0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf3,0xf2,0xf1,0xf0
TABLE1_ST  .word   TABLE1_LOC_START

RESET       mov.w   #0280h,SP               
            mov.w   #WDTPW+WDTHOLD,&WDTCTL  ; stop watchdog timer

        mov     &TABLE1_ST, R10
        mov     #0xf2, R11
        mov     #0x08, R12
again       inc     R10
        cmp     0(R10), R11
        je      found
        dec     R12
        jz      again
found       mov     R10, &f2Address

endProgram  jmp     endProgram

In an attempt to solve this problem I changed the 'je' to 'jeq' and I added 'mainLoop' to the code. After my modifications I had this code:

.label   TABLE1_LOC_START
TABLE1  .byte   0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf3,0xf2,0xf1,0xf0
TABLE1_ST  .word   TABLE1_LOC_START

mainLoop   mov    &TABLE1_ST, R10   
        mov    #0xf2, R11           
        mov    #0x08, R12           
again   inc    R10                  
        cmp    R10, R11             
        jeq    found                
        dec    R12                  
        jnz    again                 
found   mov    R10, &f2Address      

endProgram  jmp    endProgram

When I step through it, R12 eventually decrements to zero. Once it does, it means that the value of 0xf2 wasn't found, so it should place a 0x00 in the 'f2Address'. But instead of placing a zero there, it simply keeps on moving through the instructions without changing/adding any values anywhere.

I am not quite sure what to do or where to go next from here. It is using CodeComposer on the MSP430.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • I edited the problem to hopefully clarify what I need to do. – defaultname Feb 11 '16 at 21:09
  • 1
    @PeterCordes : The assembly isn't for x86, it is for the 16-bit RISC based TI processors which happens to have registers R0 to R15. Many of the assembly instructions of the MSP430 have similar names to the ones used on x86 assemblers, so it is easy to think this might be x86. – Michael Petch Feb 11 '16 at 21:14
  • 1
    @MichaelPetch: Thanks. Is there a tag for it? nvm, you already retagged while I was typing the rest of this comment. Anyway, even with the edit, I still don't see a clear description of how it goes wrong / what the OP found with a debugger. e.g. "but after this instruction, value x isn't where I expected it". So I'm leaving my downvote in place until that's improved. – Peter Cordes Feb 11 '16 at 21:25
  • @PeterCordes Yes there is a tag for it, and I amended his question with it a short time before your comment. I agree about the question being ambiguous though. – Michael Petch Feb 11 '16 at 21:26
  • Here are the exact words in the assignment: "This program is supposed to search an array of bytes for the value 0xf2. When it finds 0xf2, it should save its location (i.e. address) into the integer variable “f2Address”. If it does not find the value of 0xf2 in the array, it should put a value of 0x00 into the variable “f2Address”. This program has some bugs in it. Fix this program so that it does work properly." example: I changed the 'je' to 'jeq' and I added 'mainLoop' to it. – defaultname Feb 11 '16 at 21:30
  • 1
    @defaultname: I have added your comment to the question, but what you fail to tell us is how your solution didn't solve the problem, what does it do wrong etc. You've shown us a solution the problem but haven't made it clear how it fails (or doesn't work correctly) – Michael Petch Feb 11 '16 at 21:49
  • 1
    When I step through it, R12 eventually decrements to zero. Once it does, it means that the value of 0xf2 wasn't found, so it should place a 0x00 in the 'f2Address'. But instead of placing a zero there, it simply keeps on moving through the instructions without changing/adding any values anywhere. – defaultname Feb 11 '16 at 21:57
  • @defaultname : I added that to your question. Just to make sure you are aware, you should hit the `edit` link under the question in the future so you can add this extra information yourself. Adding information to clarify the problem is better inside the question than in the comments. – Michael Petch Feb 11 '16 at 22:13
  • 1
    @defaultname: exactly the kind of thing your question was missing before. removed my downvote. Nobody wants to debug your code from scratch for you, so you're more likely to get help if people can pick up right where you got stuck. – Peter Cordes Feb 11 '16 at 22:14

1 Answers1

3

Figured it out:

mainLoop    mov    &TABLE1_ST, R10  
        mov    #0xf2, R11       
        mov    #0x10, R12       

again   inc    R10          
        cmp    0(R10), R11      
        jeq    found                                       
        dec    R12          
        jnz    again            
        mov #0x00, &f2Address   

found   mov    R10, &f2Address  

endProgram  jmp    endProgram          
  • 1
    This does not work if 0xF2 is the first table entry. And when `endProgram` is reached, `f2Address` is never zero. – CL. Feb 12 '16 at 05:27