Home > Back-end >  Assembly Usage of the Stack
Assembly Usage of the Stack

Time:01-04

The code Should work like this: read a character from the input and look if it is a uppercase Letter repeat the reading until a non uppercase character is read in print out all the uppercase characters starting with the newest read in letter for example: A B C E f

E C B A .... my question would be how do i use the stack to find out if every letter has been printed when is the end reached. and if the first read in char is a non uppercase char it should print out an error message

Start:  
    call    read_char         ;reading a char and comparing if it is a uppercase letter
    mov     ebx,eax
    call    read_char          ;if a non uppercase letter is read in jump to print label

    cmp    ebx,65
    jb     PrintChar

    cmp   ebx,90
    ja    PrintChar
    
    push    ebx             ;pushing the char onto the stack and repeating the process
    
    jmp     Start

    
PrintChar:
loop:
    pop     eax                      ;pop the char from the stack to the eax register and print it
    call    print_char               

    jmp     loop                     ;repeat this process until all char are printed out (this code is not written)
    

MessageFail:

    mov      eax,msg_fail                          ;if the stack was empty (the first input a lovercase letter) print a fail message
    call    print_string
    call    print_nl
    jmp     Ende
Ende:

CodePudding user response:

"How do I use the stack to find out if every letter has been printed when is the end reached?"

You could always push a special character onto the stack before starting, and during your print loop you can check to see if you encounter this special character before printing. If you do, then instead jump to Ende. I would of course not use a character in the range of uppercase ASCII, as that would cause issues. Perhaps simply push 0x0 onto the stack to start with and check for that after pop eax in loop. Something like this:

Start:
    push    0x0               ; push 0x0 onto the stack to denote the beginning
ReadChars:
    call    read_char         ;reading a char and comparing if it is a uppercase letter
    mov     ebx,eax

    cmp    ebx,65
    jb     PrintChar

    cmp   ebx,90
    ja    PrintChar
    
    push    ebx             ;pushing the char onto the stack and repeating the process
    
    jmp     ReadChars

    
PrintChar:
loop:
    pop     eax                      ;pop the char from the stack to the eax register and print it

    test    eax, eax            ; test if eax=0
    jz      Ende             ; jump to end if eax was zero
    call    print_char               

    jmp     loop                     ;repeat this process until all char are printed out (this code is not written)
    

MessageFail:

    mov     eax,msg_fail                          ;if the stack was empty (the first input a lovercase letter) print a fail message
    call    print_string
    call    print_nl
    jmp     Ende
Ende:

As far as printing an error message if the first character is out of range, I think you could always use a spare register to store whether or not you are operating on the first character. Let's say you used ecx to store this. Then in Start initialize ecx to 0x0 and at the end of ReadChars update ecx to 0x1. Then, if ecx is zero and the character you read is not within the uppercase ASCII range, you know that the user entered an invalid character sequence, and you can jump to MessageFail

  •  Tags:  
  • Related