【问题标题】:Can anyone here make me understand this program and what output will I get through this?这里的任何人都可以让我理解这个程序,我会通过这个得到什么输出?
【发布时间】:2019-04-01 19:48:58
【问题描述】:
DATA SEGMENT
STRING1 DB 11H,22H,33H,44H,55H
MSG1    DB "FOUND$"
MSG2    DB "NOT FOUND$"
SE      DB 34H
DATA ENDS

PRINT MACRO MSG
    MOV AH, 09H
    LEA DX, MSG
    INT 21H
    INT 3
ENDM

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
    MOV AX, DATA
    MOV DS, AX
    MOV AL, SE
    LEA SI, STRING1
    MOV CX, 04H

UP:
    MOV BL,[SI]
    CMP AL, BL
    JZ FO
    INC SI
    DEC CX
    JNZ UP

    PRINT MSG2
    JMP END1
FO:
    PRINT MSG1
END1:
    INT 3

CODE ENDS
END START

【问题讨论】:

  • 您是否尝试在调试器中运行它以查看它的作用以及原因?
  • 是的,我尝试运行它,我也得到了输出......但我不理解这个程序,实际上如何理解我得到的输出是对还是错。
  • 我的投票相当于memchr,加上友好的输出。 man7.org/linux/man-pages/man3/memchr.3.html

标签: assembly dos x86-16 emu8086


【解决方案1】:

程序在序列 11H,22H,33H,44H 中搜索字节 '34H'。

START 部分执行以下操作:

  • 将 DS:SI 设置为 STRING1 的地址。
  • 为要搜索的字节设置 AL 为了。
  • 将 CX 设置为要在 STRING1 中搜索的字节数(4 字节)。

循环部分执行以下操作:

  • 将 DS:SI 处的字节(从 STRING1)加载到 BL 中
  • 将其与要搜索的字节(在 AL 中)进行比较
  • 如果字节相等(零标志 = 1),则打印“找到”
  • 否则(不等于)转到下一个字节(INC SI),递减计数器(CX)
  • 如果计数器为零,则退出循环并打印“NOT FOUND”

打印宏:

MOV AH, 09H
INT 21H 

是 MS-DOS 对 Print 的调用,直到 "$" INT 3 表示退出程序。

可疑的是计数器 CX 设置为 4 但 STRING1 序列包含 5 个字节。如果没有真正调试过程序,我会说 CX = 5 是正确的吗?

【讨论】:

  • 你说得对,它只搜索字节数组的前 4 个元素。如果意图是搜索整个事物,CX bein 5 是正确的。
【解决方案2】:

我的投票相当于memchr(s,c,n),其中sSI 中(每次迭代递增),cBL 中(与从 SI 获得的'item found' exit条件)和nCX 中(如果未找到等于BL 的数组元素,则减少为零,这是其他退出条件)加上友好输出(包含 int21h 调用的宏)。见http://man7.org/linux/man-pages/man3/memchr.3.html

【讨论】:

    【解决方案3】:

    它在字节数组11H,22H,33H,44H,55H中搜索值33h

    DATA SEGMENT
    # Data where we will look up for value
    STRING1 DB 11H,22H,33H,44H,55H
    # message to be printed out
    MSG1 DB "FOUND$"
    # yet one message
    MSG2 DB "NOT FOUND$"
    # program will look up for this value
    SE DB 33H
    
    DATA ENDS
    
    # this is PRINT macro- will be used later like a function
    PRINT MACRO MSG
    # 09H - is a function of interrupt 21h - print to console http://spike.scu.edu.au/~barry/interrupts.html
    MOV AH, 09H
    LEA DX, MSG
    # print message using interrupt 21h
    INT 21H
    # I think it's exit - check it in the x86 interrupts table if you need
    INT 3
    
    ENDM # end of print macro
    
    CODE SEGMENT
    
    ASSUME CS:CODE, DS:DATA
    
    START:
    
    # this is the first line of code
    MOV AX, DATA
    MOV DS, AX
    # copy value of SE (33h) to the AL register so later we can compare it
    MOV AL, SE
    LEA SI, STRING1
    
    # save in CX length of the data to be looked up for value (actually num or iterations)
    MOV CX, 04H
    
    # the main loop starts here
    UP:
    # copy byte from buffer STRING1 to the BL register
    MOV BL,[SI]
    # check if AL==BL -> value found
    CMP AL, BL
    
    # if AL==BLjump to FO (will print FOUND)
    JZ FO
    # if not found, 
    # move to next byte in STRING1
    INC SI
    # decrement loop constraint (loop should end before reaching end of string)
    DEC CX
    # if CX <> 0 loop again
    JNZ UP
    # if CX==0 print message NOT FOUND
    PRINT MSG2
    JMP END1
    FO:
    PRINT MSG1
    END1:
    INT 3
    CODE ENDS
    
    END START
    

    【讨论】:

      猜你喜欢
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 2011-07-22
      相关资源
      最近更新 更多