在英特尔8086组件中将小写转换为大写。如果已经处于大写状态DO_NOTHING并继续保存

时间:2012-10-13 00:36:05

标签: assembly uppercase x86-16

;我创建了这个程序(英特尔8086程序集)来读取字符并存储它们并打印;它们在转换为大写字母后以相反的顺序。我知道你可以转换为upper; case使用(sub AL,20h)但是我如何设置条件(IF)循环?

org 100h
mov BX, 0800h
mov DS, BX      ;Set segment register
mov BX, 0000h   ;Set offset register
mov CX, 0       ;Set counter to 0      

;mov CH, 0

readwrite:      ;Top of read/write loop
mov AH, 01h     ;Get ready to read a character
int 21h         ;Read character into AL 

;jb readwrite

mov [BX], AL    ;Move AL into memory at DS:BX

add CX, 1       ;Increment Counter 
add BX, 1       ;Increment offset      

cmp AL, '$'     ;Compare entered to $
jg readwrite    ;if (C > 0) loop



;Reverse Printing

mov BX, 0800h
mov DS, BX      ;Set segment register
mov BX, 0000h   ;Set offset register            
add BX, CX
sub BX, 2            

mov AH, 0eh  

mov AL, 010 ;NL
int 10h
mov AL, 013 ;CR  
int 10h



revprinting:
mov AL, [BX]
int 10h    
sub BX, 1
sub CX, 1
cmp CX, 1
jg revprinting

ret

2 个答案:

答案 0 :(得分:2)

如果您知道您的字节是字母(不是0..9或其他非字母),则可以跳过IF部分。

您可以无条件地设置或清除Bit Nr,而不是使用20h作为ASCII中大写和小写的区别。 5(ASCII字符:位Nr.7,...,位Nr.0)。

通过将ASCII字符的第5位设置为1,您将得到一个小写字母。通过将位5设置为0,您将获得大写字母。 (http://asciitable.com/

获取大写字母。通过ANDing清除位5到0 ,并使用设置了所有其他位的掩码:

and al, 0xdf   ; you may use 0dfH instead of 0xdf in your notation, or ~0x20

获得小写。将位5设置为1:

or al, 0x20    ; 20h

这对于检查字符是否是字母也很有用。您只需将所有字母设置为大写。之后,用“cmp”检查角色是在“A”之下还是在“Z”之上。如果没有,那就是一封信。甚至更好,

and   al, 0xdf      ; force uppercase (if it was a letter to start with)

sub   al, 'A'       ; letters turn into AL in [0..25]
cmp   al, 'Z'-'A'
ja   non_letter     ; unsigned compare: lower ASCII codes wrap to high values

答案 1 :(得分:0)

要有条件地进行转换,您通常会使用几个比较:

    cmp al, 'a'
    jl  not_lower
    cmp al 'z'
    jg  not_lower

    sub al, 20h

not_lower:

根据具体情况,还有另一种可能性更可取:

    mov bl, al
    sub bl, 'a'
    cmp bl, 'z'-'a'   ; 25
    ja  not_lower

    sub al, 20h

not_lower:

让你只用一个jmp代替两个,并且有助于使它更具可预测性。

相关问题