Dmitriy78 0 Newbie Poster
;Dmitriy Potemkin 816 Assignment 3
      BR      main        
retVal:  .Equate 2

printMes:STRO    msg1,d      
         CHARO   '\n',i
         STRO    msg2,d      
         CHARO   '\n',i     
         STRO    msg3,d      
         CHARO   '\n',i
         STRO    msg4,d
         CHARO   '\n',i  
         DECI   retVal,s
         RET0
;*******************************************************************  second procedure
str1:    .Equate 0
rett2:   .Equate 3
max2:    .Equate 5
sent2:   .Equate 7
retVal2: .Equate 9

secPro:  SUBSP   1,i
         LDA     rett2,s
         CPA     1,i
         BREQ    c1
         LDA     rett2,s
         CPA     2,i
         BREQ    c2
         LDA     rett2,s
         CPA     3,i
         BREQ    c3
         LDA     rett2,s
         CPA     4,i
         BREQ    c4


c1:      STRO    msgIn,d
         LDA     0,i
         STA     retVal2,s
         CHARO   '\n',i      ;This part is present in every procedure, just to see what has been passed to the procedure.
         CHARO   sent2,s
         CHARO   '\n',i
         DECO    rett2,s
         CHARO   '\n',i
         DECO    max2,s
         CHARO   '\n',i
while:   CHARI   str1,s
         LDBYTEA str1,s
         CPA     sent2,s
         BREQ    ender
         LDA     retVal2,s
         ADDA    1,i
         STA     retVal2,s
         LDA     retVal2,s   ;Here we check if the number of chars entered is at the max or more, if yes then it returns the maximum number.
         CPA     max2,s
         BRGE    ender
         br      while

c2:      STRO    msgIn,d
         LDA     0,i
         STA     retVal2,s
         CHARO   '\n',i
         CHARO   sent2,s
         CHARO   '\n',i
         DECO    rett2,s
         CHARO   '\n',i
         DECO    max2,s
         CHARO   '\n',i
while1:  CHARI   str1,s
         LDBYTEA str1,s
         STA     str1,s
         LDBYTEA str1,s
         CPA     sent2,s
         BREQ    ender
         LDBYTEA str1,s
         CPA     'a',i
         BREQ    ca
         CPA     'e',i
         BREQ    ce
         CPA     'i',i
         BREQ    ci
         CPA     'o',i
         BREQ    co
         CPA     'u',i
         BREQ    cu
ca:      LDA     retVal2,s
         ADDA    1,i
         STA     retVal2,s
         br      while1
ce:      LDA     retVal2,s
         ADDA    1,i
         STA     retVal2,s
         br      while1
ci:      LDA     retVal2,s
         ADDA    1,i
         STA     retVal2,s
         br      while1
co:      LDA     retVal2,s
         ADDA    1,i
         STA     retVal2,s
         br      while1
cu:      LDA     retVal2,s
         ADDA    1,i
         STA     retVal2,s
         LDA     retVal2,s
         CPA     max2,s
         BRGE    ender
         br      while1

c3:      STRO    msgIn,d
         LDA     0,i
         STA     retVal2,s
         CHARO   '\n',i
         CHARO   sent2,s
         CHARO   '\n',i
         DECO    rett2,s
         CHARO   '\n',i
         DECO    max2,s
         CHARO   '\n',i 
while3:  CHARI   str1,s
         LDBYTEA str1,s
         STA     str1,s
         LDBYTEA str1,s
         CPA     sent2,s
         BREQ    ender
         LDA     str1,s
         CPA     'a',i
         BREQ    while3
         LDA     str1,s
         CPA     'e',i
         BREQ    while3
         LDA     str1,s
         CPA     'i',i
         BREQ    while3
         LDA     str1,s
         CPA     'o',i
         BREQ    while3
         LDA     str1,s
         CPA     'u',i
         BREQ    while3
         LDA     retVal2,s
         adda    1,i
         STA     retVal2,s
         LDA     retVal2,s
         CPA     max2,s
         BRGE    ender
         br      while3

c4:      STRO    msgIn,d          
         LDA     0,i
         STA     retVal2,s
         CHARO   '\n',i
         CHARO   sent2,s
         CHARO   '\n',i
         DECO    rett2,s
         CHARO   '\n',i
         DECO    max2,s
         CHARO   '\n',i
while4:  CHARI   str1,s      
         LDBYTEA str1,s
         CPA     'z',i
         BRGT    ender
         LDBYTEA str1,s
         CPA     'A',i
         BRLT    ender
         LDA     retVal2,s
         adda    1,i
         STA     retVal2,s
         LDA     retVal2,s
         CPA     max2,s
         BRGE    ender
         br      while4


ender:   CHARO   'E',i       ;prints out end and returns to main procedure where the number of chars is printed
         CHARO   'n',i
         CHARO   'd',i
         CHARO   '\n',i
         RET1    



;*******************************************************************  main
sent1:   .EQUATE 0
max1:    .Equate 1
returned:.Equate 3
junk:    .Equate 5

main:    subsp   5,i
         STRO    msgEnd,d
         CHARI   sent1,s
         CHARI   junk,s
         STRO    msgLim,d
         DECI    max1,s
         subsp   2,i ;for return value and parameters only.
         CALL    printMes
         addsp   2,i
         LDA     -2,s
         STA     returned,s

         LDA     returned,s
         STA     -7,s

         LDA     max1,s
         STA     -5,s

         LDBYTEA sent1,s
         STBYTEA -3,s

         subsp   7,i
         CALL    secPro
         addsp   7,i

         LDA     -1,s
         STA     returned,s
         DECO    returned,s

         addsp   5,i


         STOP     

msgTotal:.ASCII  "The total number of Chars is. \x00"           
msgIn:   .ASCII  "Input your sentence. \x00"
msgEnd:  .ASCII  "Enter the sentinel character to use. \x00"
msgLim:  .ASCII  "Enter the max amount of characters to use. \x00"
msg1:    .ASCII  "1. Total number of characters\x00"     
msg2:    .ASCII  "2. Number of vowels\x00"    
msg3:    .ASCII  "3. Number of consonents\x00"   
msg4:    .ASCII  "4. Number of non-alphabetic characters\x00"   

         .END      

This program is supposed to take in a sentinel character and a maximum number of characters and then choose one of 4 options (counting the vowels, consonents, etc.). Whenever it starts counting though in "secPro:" it enters an endless loop where it compares the input character to god knows what (the value in the accumulator is odd). Please help.

Dani AI

Generated

Likely cause: the procedure frame and addressing modes are out of sync, so the sentinel/character comparisons are reading garbage and the loop never terminates. In the posted code the callee only reserves a tiny stack area on entry but then accesses locals/parameters at offsets up to 9; that mismatch will make CHARI/load/compare instructions fetch whatever happens to be on the stack instead of the intended values. Also check byte vs word operations — using word loads/stores where a byte (or an address) is expected (or vice‑versa) will produce the same symptom.

Fixes to try (concrete, low‑risk changes):

  • Make the callee reserve enough stack bytes at its prologue: allocate at least the highest offset used plus the size of that slot. If offsets run 0..9 and those are byte slots, reserve 10 bytes; if some slots are words reserve 2× as many bytes for those. Keep caller and callee consistent about whether a parameter is a byte, a word, or an address.
  • Be consistent about byte vs word instructions: use the byte forms (LDBYTE/LDBYTEA/STBYTE/STBYTEA, CHARI) for single characters, and LDA/STA for word values (integers returned with DECO/DECI). A mismatch here will make CPA compare the wrong value.
  • When passing a buffer/pointer, make sure the callee receives an actual address in the location it expects (store pointer vs store byte are different operations).

Debugging tips:

  • Instrument the prologue of the procedure to print the numeric value of the sentinel and the first read character (use DECO/CHARO) before entering the loop to confirm what the loop actually sees.
  • Temporarily simplify the loop to read one character, print it and return. If that behaves correctly, add back counting logic step by step.
  • Use the simulator’s memory inspector to watch the stack region (SP and nearby addresses) right before/after the CALL and after the callee’s SUBSP to verify that variables live where the code expects them.

Common pitfalls to recheck: mismatched SUBSP sizes between caller and callee, confusion between a buffer and a pointer to a buffer, and mixing byte/word instructions. Fixing the stack allocation and the byte/word consistency will almost certainly stop the “weird accumulator / endless loop” behavior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.