Home > Software design >  Print to the console the entire alphabet of uppercase and lowercase (A to Z, a to z) with custom for
Print to the console the entire alphabet of uppercase and lowercase (A to Z, a to z) with custom for

Time:11-24

How can I print all the alphabet characters (A to Z, a to z)?

I did the following. It lets me print all the characters from A-Z and a-z but the problem is that I get the in-between ASCII characters that I want to filter.
CX is expected to be set to 52 in a working program.

ORG 100h       
MOV AX,0B800H  
MOV DS,AX
MOV BX,0000    ;SET BX REGISTER
MOV CX, 58      ;USED AS A COUNTER 
MOV AL,'A'
MOV AH,0x0E
MOV DL, 41h  
CMP AX, DX
JG UPPERCASE 

UPPERCASE:
    ADD DL, 1
    CMP DX, 5Bh
    JG LOWERCASE
    JMP BACK
    
LOWERCASE:
    ADD DL, 1
    CMP DX, 60h
    JG BACK
    JL UPPERCASE

HLT

BACK:
  MOV [BX],AX
  ADD AL,1
  ADD AH,1
  ADD BX,2
  LOOP UPPERCASE

CodePudding user response:

I get the in-between ASCII characters that I want to filter

That's because your logic that tries to skip the range from [91,96] increments the parallel counter DL but forgets to also increment the ASCII code in AL. Of course you do not need that DL counter since AL can serve that purpose already!

CMP AX, DX
CMP DX, 5Bh
CMP DX, 60h

You only ever initialize DL (MOV DL, 41h), so comparing to DX is like playing Russian roulette. DH is not necessarily zeroed.

JG UPPERCASE 

UPPERCASE:

This construct is non-sense in assembly programming. If the condition is greater then you jump to UPPERCASE, but if the condition is not greater then you fall-through in UPPERCASE. No matter, you will always run the code at UPPERCASE. In this program that is what you want to happen, but not in that manner.


Using 2 loops

  ORG  256
  mov  ax, 0B800h  ; Output via ES:DI
  mov  es, ax
  xor  di, di
  cld
  mov  ax, 0E41h   ; YellowOnBlack capital 'A'
firstLoop:
  stosw
  add  ax, 0101h   ; Increment Attribute and ASCII together
  cmp  al, 'Z'
  jbe  firstLoop
  mov  al, 'a'     ; Skip the in-between characters
secondLoop:
  stosw
  add  ax, 0101h
  cmp  al, 'z'
  jbe  secondLoop
  ret

Using 1 loop

  ORG  256
  mov  ax, 0B800h  ; Output via DS:BX
  mov  ds, ax
  xor  bx, bx      ; Better than `mov bx, 0`
  mov  ax, 0E41h   ; YellowOnBlack capital 'A'
oneLoop:
  mov  [bx], ax
  add  bx, 2
  add  ax, 0101h   ; Increment Attribute and ASCII together
  cmp  al, 'Z'   1
  je   skip
  cmp  al, 'z'
  jbe  oneLoop
  ret

skip:
  mov  al, 'a'     ; Skip the in-between characters
  jmp  oneLoop

CodePudding user response:

Thanks everyone for the tips. I ended up with something like this and I can see how my logic was overly complicated. The genius is in its simplicity!

My Solution:

include emu8086.inc
ORG 100h       
MOV AX,0B800H  
MOV DS,AX
MOV BX,0000     ;SET BX REGISTER
MOV CX, 52      ;USED AS A COUNTER 
MOV AL,'A'
MOV AH,0x0F           
CMP AL, 41h
JMP BACK

BACK:
    CMP AL,5Bh
    JE NOTLETTER

    JMP GO

GO:   
    MOV [BX],AX
    ADD AL,1
    ADD AH,17
    ADD BX,2
    LOOP BACK

HLT
  
NOTLETTER:
    CMP AL, 61h
    MOV AL, 61h
    printn " "
    JMP GO 
  • Related