循环检查装配小写字母 [英] Loop to check lowercase letter in assembly

查看:218
本文介绍了循环检查装配小写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个环路来检查,如果第二个参数包含任何大写字母(如果是,程序退出)一个更大的计划的一部分。这里是我到目前为止所。我已标记code我有问题的部分。

I'm writing a loop as a part of a bigger program that checks if 2nd argument contains any uppercase letter (if yes, program quits). Here's what I have so far. I have marked the section of code I am having problems with.

%include "asm_io.inc"
SECTION .data
err1: db "Incorrect number of command line arguments",10,0
err2: db "2nd argument lenght more than 20",10,0
err3: db "Upper-case letter found!!!",10,0
strt: db "ho",10,0

SECTION .bss
N: resd 1

SECTION .text 
    global  asm_main

asm_main:
   enter 0,0
   pusha

   mov eax, dword [ebp+8]
   cmp eax, dword 2
   jne ERR1

   mov ebx, dword [ebp+12]   ; 24 to 37 calculates the length of 2nd argument
   mov eax, dword [ebx+4]
   mov edi, eax
   sub ecx, ecx
   sub al, al
   not ecx
   cld
   repne scasb
   not ecx 
   dec ecx
   mov eax, ecx
   cmp eax, dword 20
   ja ERR2

   mov [N], dword eax       ;line 39 to 54 makes sure that all letters are lowercase
   call print_int
   mov ebx, N
   call print_int
   mov ebx, dword [ebp+12]
   mov eax, dword [ebx+4]

;------------------------- Code that I am having problems with
LOOP: 
   mov bl, byte[eax]
   cmp bl, 0x61
   jb ERR3
   cmp bl, 0x7A
   jb ERR3
   add eax, 4
   add ecx,1
   cmp ecx, N
   jb LOOP
;---------------------------------------------------------------

   mov eax,  0
   mov eax, strt
   call print_nl
   call print_string
   jmp asm_main_end

 ERR1:
   mov eax, err1
   call print_string
   jmp asm_main_end

 ERR2:
   mov eax, err2
   call print_string
   jmp asm_main_end

 ERR3: 
   mov eax, err3
   call print_string
   jmp asm_main_end

 asm_main_end:
   call print_nl
   popa                  
   leave                     
   ret

不过,即使我的第二个参数是 ABCD ,环路仍然跳转到 ERR3 。任何想法我可能是做错了?我运行RedHat Linux的,NAS 07年10月2日。
编辑:N是这是正确的第二个参数的长度。

But even if my 2nd argument is abcd, the loop still jumps to ERR3. Any ideas what I might be doing wrong? I'm running redhat-linux, nas 2.10.07. N is the length of the 2nd argument which is correct.

推荐答案

我认为你正在寻找的东西,如:

I think you are looking for something like:

  xor ecx, ecx   ; Initialize the counter ECX to zero
LOOP:
  mov bl, byte[eax]
  cmp bl, 'A'
  jb .CONT       ; If we are below 'A' we are not a capital, continue to next char
  cmp bl, 'Z'    ; We are >= 'A' here, but we need to check if we are <= 'Z' 
  jbe ERR3       ; If we are <= 'Z' then we must be a capital so Goto ERR3
.CONT:           ; If there was no error we reach here
  inc eax        ; Goto next character
  inc ecx        ; We want to do another loop
  cmp ecx, [N]   ; Compare to what is variable [N], not its pointer
  jbe LOOP       ; Loop while ECX <= N (N = strlen not including nul terminator) 

我已经放在注释到code解释。这code还没有被精简,我试图产生code以类似的方式到code你正在写。

I've placed comments into the code to explain. This code hasn't been streamlined, I'm trying to produce code in a similar fashion to the code you are currently writing.

总的想法是'A'和'Z'的范围之间的比较。如果我们在这个范围内则有一个错误。首先比较,如果我们不到'A'。如果我们不能成为一个大写字母,因为下面还有'A'(在ASCII表)无大写。如果我们均低于'A'我们只是继​​续到下一个字符。如果我们不能低于'A',我们继续检查,看看是否我们实际上&LT; ='Z'。如果我们那么我们必须是一个大写字母,然后我们产生错误。否则,我们继续到下一个字符。

The general idea is to compare between the range of 'A' and 'Z'. If we are in that range then we have an error. First compare if we are less than 'A'. If we are we can't be a capital letter, because there are no capitals below 'A' (in the ASCII table). If we were below 'A' we just continue to the next character. If we were not below 'A' we continue checking to see if we are in fact <= 'Z'. If we are then we must be a capital letter and then we produce an error. Otherwise we continue to the next character.

(我上面提到的)更先进的方法是从的 BL 的减'A',然后比较,为25('Z' - 'A')使用无符号比较。如果的 BL 的低于或等于25则的 BL 的是一个大写字母。在code可能是这个样子的:

A more advanced method (one I alluded to above) is to subtract 'A' from BL and then compare that to 25('Z'-'A') using unsigned comparison. If BL is below or equal to 25 then BL was a capital letter. The code could have looked like this:

     xor ecx, ecx
   LOOP:
     mov bl, byte [eax]
     sub bl, 'A'              ; Normalize BL
     cmp bl, 'Z'-'A'          ; 'Z'-'A' = 25 (alphabet between 0 and 25 inclusive)
     jbe ERR3                 ; If BL <= 25 (unsigned comparison!) then BL 
                              ; was a capital letter

     inc eax
     inc ecx
     cmp ecx, [N]
     jbe LOOP  

这篇关于循环检查装配小写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆