GIDForums  

Go Back   GIDForums > Computer Programming Forums > Assembly Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 21-Feb-2009, 04:40
PaulGarcha PaulGarcha is offline
New Member
 
Join Date: Feb 2009
Posts: 12
PaulGarcha is on a distinguished road

Re: ASM program


Nope, im trying to edit it to do my thing so i can check for multiple characters.
Yes theres a test in place for char and im trying to get it to check char2 by copying some of the code/loops but im failing even after reading various tutorials
  #12  
Old 21-Feb-2009, 14:29
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: ASM program


hmmm , well they say a picture is worth a thousand words so here are both!
I indented and redid the comments in the code to try to better note what is going on and point out suggestions to help YOU insert code to do what you want.
CPP / C++ / C Code:
## A program for the MIPS emulator: "PCSpim" 
##
## Objective: 
## Count the occurance of ascii byte 'char' in an ascii string.
##
## t0 - holds each byte from string in turn
## t1 - index into array: k in str(k)
## t2 - count of occurences
## t3 - holds the character to search for
##
.data
    str: .asciiz "abcd"    # this is the target string to search
   char: .asciiz "a"       # this is the original target character       
  char2: .asciiz "z"       # this is the target char your trying to implement       
   txt1: .asciiz "\n\n the number of "           # report message string
   txt2: .asciiz " in the above string is:: "    # message string
   endl: .asciiz "\n Bye, please test again! \n" # message string

.text
.globl main
main:
  nop                   # no operation 'instruction'
  li  $t1, 0            # $t1 holds the array index
  li  $t2, 0            # $t2 holds n_timesfound counter
  lb  $t3, char         # $t3 holds the search-for character
                        # MAYBE A SECOND SEARCH-FOR ASSIGNMENT TO $t4 ??
#
  loop:
    lb   $t0, str($t1)  # load byte from address  str($t1)  into $t0
    beqz $t0, strEnd    # if NULL, goto "strEnd" label  (exit loop)

                        #### THIS IS WHERE YOUR COUNT IS INCREMENTED ####
    bne  $t0, $t3, con  # if $t0 != $t3 (char), breakto "con" label
                        # which is essentially "if != skip the next line"
                        # which is essentially "if == DO the next line"
    add  $t2, $t2, 1    # increment counter (t2)
    con:                # the 'break to'  label
                        # OK SO YOU JUST NEED TO DO ANOTHER CONDITIONAL
                        # LIKE THE ONE IN THE LAST THREE LINES FOR char2 
                        # YOU WILL ALSO NEED TO WORK IT INTO  
                        # THE REPORT PRINTING ROUTINE BELOW 
                        #################################################
                        
    add  $t1, $t1, 1    # increment index
  j loop                # "jump" to (goto) the "loop" label
#
  strEnd:             # string search all done so deliver output

                      # PRINT THE TARGET STRING 
  la $a0, str         # load string address represented by str into register a0
  li $v0, 4           # load int syscallcode 4 (print string) into register v0 
  syscall             # interrupt kernel to do syscall 4

                      # PRINT FIRST PART OF OUTPUT MESSAGE
  la $a0, txt1        # load string address of txt1 into register a0
  syscall             # syscall  (syscallcode 4 (print string) is still in v0 ) 

                      # PRINT SINGLE SEARCH-FOR CHARACTR
  la $a0, char        # load string address represented by "char" into a0
  syscall             # syscall  (syscallcode 4 (print string) is still in v0 ) 
  
                      # PRINT CENTER PART OF MESSAGE 
  la $a0, txt2        # load string address represented by "txt2" into a0
  syscall             # syscall  (syscallcode 4 (print string) is still in v0 ) 

                      # PRINT THE N_TIMES SEARCH-FOR CHARACTER WAS FOUND
  move $a0, $t2       # move value found in register t2 (n_timesfound) into a0
  li   $v0, 1         # load the callcode to print an integer (1) into v0
  syscall             # interrupt kernel to do syscall 1

                      # PRINT THE CLOSING MESSAGE LINE
  la $a0, endl        # load string address of endl into register a0
  li $v0, 4           # change callcode in v0  back to 4 (print string)
  syscall             # interrupt kernel to do syscall 4

  li $v0, 10          # (usual quit)  (SYSCODE TO EXIT)
  syscall             # au revoir...
## THE END
Code:
These are the steps I use to view the program in PCSpim 1) Open .asm file in my editor. 2) Open PCSpim. 3) goto > Simulator > Settings In the bottom section ONLY: "Allow pseudo instructions" and "Mapped I/O" are checked. 4) Set up the screen: - I maximize the main PCSpim window. - At the top I have the "Registers" window sized to show all 32 registers. - Next I have the "Text Segment" window full width showing 5 or 6 lines. - Next I have the "Data" window, - and at the bottom the messages window. (I note that the data window will sometimes not re-show data if if another window enlarged to overlap it and then reduced... ) 5) Open the .asm to run. File > Open > select... As it loads you should see all 4 of those windows load up with info. The Register and Text windows are of particular interest, so get to know them. I consult the Data , Messages and "Console" (not opened yet) windows as needed. The console window is only to show actual program output and 'get' input. 6) Next we need to tell PCSpim where to start running the code. Note that in the Text window the first line is the code address: 0x00400000. So we need to set that address as the starting point. Simulator > Set Val - in top enter: PC (or pc ) - in bottom enter: 0x00400000 (or 0x400000 ) 7) Hit F10 to do each step of code. Observe value changes in ALL windows. Open console if needed. I note that it closes if another step is done, but on re-oopening the previous display is retained. 8) After last line is executed you will need to re-"Set Value" the code pointer. ----- Now I'll step through the code above: line 22- Register: PC gets our code start address value... NOP is done (or not) The next line 25 of code: "li $t1, 0" is highlighted int Text window indicating that it is the next to be executed. 23-not much to see in Registers window since we're assigning zeros to t1 and t2 24- ... 25- value 61 goes into t3 (see it?) 29- value 61 goes into t0 30- ... 33- note that this time the test is false as t3 == t1 so we go to next line instead of skipping it. 36- 44- Note that as a result of these t1 and t2 are incremented in Reg. window. 45- here we'll jump up ... watch 29- t0 changes to next string character 30- 33- skip 36 44- loop again and again note that t0 will change to each successive character in string and that t1 (indice value) increments with each loop etc... Watch fo t0 to be zero .... and line 30 beqz causes a jump to: 50- note the address of str shows up in register a0 Look at the data window and see that that address matches the address to the left of the ascii code byte values 0x64636261 0x...... 51- then see the opcode 4 go into v0 55- syscall ! Now open the Window > Console and see the output. cool note that next f10 will cause it to disappear 5.... Note the address changes in v0 as each string is loaded for output. See if you can locate them within the data in the Data window. Take a final look at the output after last of code is executed...
I hope this helps you, I can't do it for you. I would still suggest you go through the tutorial one section at a time and do all the exercises. You will need to do that if you REALLY want to learn this (as you said at the very beginning).
  #13  
Old 21-Feb-2009, 15:06
PaulGarcha PaulGarcha is offline
New Member
 
Join Date: Feb 2009
Posts: 12
PaulGarcha is on a distinguished road

Re: ASM program


I understand you saying you cant do it for me but i appreciate the help you are giving with the comments in the code etc

I will try giving this another shot right now to see if i can get it to work again.
  #14  
Old 21-Feb-2009, 15:34
PaulGarcha PaulGarcha is offline
New Member
 
Join Date: Feb 2009
Posts: 12
PaulGarcha is on a distinguished road

Re: ASM program


loop:
lb $t0, str($t1) # load byte from address str($t1) into $t0
beqz $t0, strEnd # if NULL, goto "strEnd" label (exit loop)

#### THIS IS WHERE YOUR COUNT IS INCREMENTED ####
bne $t0, $t3, con # if $t0 != $t3 (char), breakto "con" label
# which is essentially "if != skip the next line"
# which is essentially "if == DO the next line"
add $t2, $t2, 1 # increment counter (t2)

con: # the 'break to' label
# OK SO YOU JUST NEED TO DO ANOTHER CONDITIONAL
# LIKE THE ONE IN THE LAST THREE LINES FOR char2
# YOU WILL ALSO NEED TO WORK IT INTO
# THE REPORT PRINTING ROUTINE BELOW

You know in that bit,what do you mean by the last 3 lines? all these as in the last 3 lines of code?

beqz $t0, strEnd
bne $t0, $t3, con
add $t2, $t2, 1

:s
  #15  
Old 21-Feb-2009, 16:31
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: ASM program


Now who can read that! Please enclose your code in the c++ tags to preserve indents!!!
Quote:
I will try giving this another shot
That's the spirit!

In regards to your last question: No, I refer to the code lines within what I sectioned out.
Including the 'con' label:
CPP / C++ / C Code:
                        #### THIS IS WHERE YOUR COUNT IS INCREMENTED ####
    bne  $t0, $t3, con  # if $t0 != $t3 (char), breakto "con" label
                        # which is essentially "if != skip the next line"
                        # which is essentially "if == DO the next line"
    add  $t2, $t2, 1    # increment counter (t2)
    con:                # the 'break to'  label
                        # OK SO YOU JUST NEED TO DO ANOTHER TEST LIKE
                        # THE ONE IN THE LAST THREE LINES FOR char2 
                        # YOU WILL ALSO NEED TO WORK IT INTO  
                        # THE REPORT PRINTING ROUTINE BELOW 
                        #################################################
Know what I mean?
The 'bne' means break to the 'con' label if the two values are not equal ... so
If you don't have a 'z' skip to the label on the other side of the next line. (don't increment 'z' counter)
OTHERWISE you DO have a 'z' so you drop right through to the next line which increments the 'z' counter.
You will need another 'skip to' label like con (maybe con2 ??? It's up to you...)
it's like
CPP / C++ / C Code:
if( $to != $t3 )
{
  /* skip the else (do nothing)   */
}
else
{ 
  $t2++;
}
Last edited by Howard_L : 21-Feb-2009 at 17:05.
  #16  
Old 21-Feb-2009, 17:02
PaulGarcha PaulGarcha is offline
New Member
 
Join Date: Feb 2009
Posts: 12
PaulGarcha is on a distinguished road

Re: ASM program


Oh i think i know what you mean
I have now created a 2nd loop and 2nd con for the 'z' letter.
I have a new counter for 'z' aswell

Im just trying to get it to scan the string as at the moment it doesnt work even if i stick a 'z' in the str
  #17  
Old 21-Feb-2009, 20:05
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: ASM program


You don't need a second loop, just a second test within the first loop.
You just do two test/counts instead of one while at each character of the string.
Post you code if you're really having trouble.
Use indents etc. and don't forget to enclose code in C++ tags.
(Check your message after posting. You have 90 minutes to edit it.)
btw Just curious, are you going to be programming for any particular device?
  #18  
Old 22-Feb-2009, 05:59
PaulGarcha PaulGarcha is offline
New Member
 
Join Date: Feb 2009
Posts: 12
PaulGarcha is on a distinguished road

Re: ASM program


ok for some reason im getting an error when i start with these....cant the registers go above $t9?
CPP / C++ / C Code:
lb $t10, char4
li $t11,0 #counter d
li $t12,0 #array 4


if not, where do i go now? Im trying to add char4, char5 etc
  #19  
Old 22-Feb-2009, 09:42
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: ASM program


Well I haven't tried using those higher registers but I'd think you would be able to use them.
Maybe your problem is elsewhere. PCSpim is picky.

Privately you sent me a version where you thought you had the loop correct.
I found that it had some problems and I note them here (just the loop section):
CPP / C++ / C Code:
        I'LL USE  #~  TO NOTES MY COMMENTS IN THE FOLLOWING:
main:
  nop                   # no operation 'instruction'
  li  $t1, 0            # $t1 holds the array index
  li  $t2, 0            # $t2 holds n_timesfound counter
  lb  $t3, char         # $t3 holds the search-for character
  lb  $t4, char2         # MAYBE A SECOND SEARCH-FOR ASSIGNMENT TO $t4 ??
  li  $t5, 0		# next counter for b
  li  $t6, 0		
 
#
  loop:
    lb   $t0, str($t1)  # load byte from address  str($t1)  into $t0
    beqz $t0, strEnd    # if NULL, goto "strEnd" label  (exit loop)

                        #### THIS IS WHERE YOUR COUNT IS INCREMENTED ####
    bne  $t0, $t3, con  # if $t0 != $t3 (char), breakto "con" label
                        # which is essentially "if != skip the next line"
                        # which is essentially "if == DO the next line"
    add  $t2, $t2, 1    # increment counter (t2)
    con:                # the 'break to'  label
                        # OK SO YOU JUST NEED TO DO ANOTHER CONDITIONAL
                        # LIKE THE ONE IN THE LAST THREE LINES FOR char2 
                        # YOU WILL ALSO NEED TO WORK IT INTO  
                        # THE REPORT PRINTING ROUTINE BELOW 
                        #################################################
                        
    add  $t1, $t1, 1    # increment index #~ <-- NOT HERE! DO IT JUST 
                                          #~ <-- THE END OF LOOP: 'j loop'
    bne $t0, $t4, con2  #~ <-- TEST2- OK 
      add $t5,$t5,1     #~ <-- INCREMENT COUNT2- OK
                        #~ <-- MOVE CON2 TO HERE!!! 
  j loop                # "jump" to (goto) the "loop" label #~ <-- OK
    
    con2:             #~ <-- SHOULD IMMEDIATELY FOLLOW THE INCREMENT OF COUNT2
    add $t6,$t6,1     #~ <-- IS THIS A SECOND INCREMENT?  NOT NEEDED... 
  j loop              #~ <-- NOT NEEDED USE THE MECHANICS OF THE FIRST LOOP

  strEnd:             # string search all done so deliver output  #~ <-- OK
When I ran the above, I found that it ran past the first jump to the second.
So you were lucking out. I don't know why you keep going outside the loop.

The construct is fairly simple:
CPP / C++ / C Code:
To search for a second character your loop should resemble this illustration:
(quasi C style:) 

## set some data:
char1= a, char2= b, string = "\n abcde";             ## btw:   !=   means "not equal to" 

## Set the registers:
$t0 = 0            # will be assigned each succesive char of string in loop
$t1 = 0            # holds the array index
$t2 = 0            # holds n_timesfound-1 counter
$t3 = char1        # holds the search-for-1 character
$t4 = 0            # holds n_timesfound-1 counter
$t5 = char2        # holds the search-for-2 character

loop:
{
    get next char from string into t0
    test t0 for zero (terminator) if yes goto "END" 
  
    test: if( t0 != t3 ) skip to "con1"                 ## <-- FIRST SEARCH
    {
        no skip so this line increments t2 (n_timesfound-1)
    }
    con1:

    test: if( t0 != t5)  skip to "con2"                 ## <-- SECOND SEARCH
    {
        no skip so this line increments t4 (n_timesfound-2)
    }
    con2:
  
} jump to "loop"                                     ## ONLY ONE LOOP USED 
END:
If you're still having problems post your latest code here please. (your indents are getting better)
Last edited by Howard_L : 22-Feb-2009 at 10:31.
  #20  
Old 22-Feb-2009, 10:04
PaulGarcha PaulGarcha is offline
New Member
 
Join Date: Feb 2009
Posts: 12
PaulGarcha is on a distinguished road

Re: ASM program


nevermind iv started using registers from a different name as £tx was full
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 04:48
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.