【问题标题】:assembly program that have two integer array X and Y of size 10 where Y contains: one if the element in X is positive zero if that element is < = 0具有两个大小为 10 的整数数组 X 和 Y 的汇编程序,其中 Y 包含:如果 X 中的元素为正,则为 1,如果该元素 < = 0,则为 0
【发布时间】:2021-12-19 10:52:18
【问题描述】:

具有两个大小为 10 的整数数组 X 和 Y 的汇编程序,其中 Y 包含: 如果 X 中的元素为正,则为 1,如果该元素

.MODEL SMALL
.DATA
x db 10 dup ( ? )
y db 10 dup ( ? )
.CODE
mov si,offset x
mov di,offset y
main1:
mov ah,01h
int 21h
mov bh,0
mov bl,al 
sub bl,30h
cmp bl,0
ja l1
jmp l2
l1:
 mov ah , 1
mov [si] , al
inc si
jmp l3

l2 : mov ah , 0
mov [di] , al
inc di
jmp l3

     

l3 :

mov dl,al
MOV AH, 02h
INT 21H
cmp si,10
je l4
cmp di,10
je l4
jmp main1

l4 : 
.EXIT
END

【问题讨论】:

    标签: assembly x86-16 signed emu8086


    【解决方案1】:

    你采纳了我来自my previous answer的一些建议,但忽略了其他的!

    您使用sub bl,30h 意味着您希望输入是数字[0,9]。没有数字会是负数!

    .MODEL SMALL
    .DATA
    x db 10 dup ( ? )
    y db 10 dup ( ? )
    .CODE                <<<< Where's initializing DS ?
    mov si,offset x
    mov di,offset y
    main1:
    mov ah,01h
    int 21h
    mov bh,0             <<<< (*) Where's storing the original value in the x array ?
    mov bl,al 
    sub bl,30h
    cmp bl,0
    ja l1                <<<< Has to be SIGNED comparison, use JGE
    jmp l2
    l1:
     mov ah , 1
    mov [si] , al        <<<< The 1 is in AH, use AH instead of AL
    inc si               <<<< Store in the y array and increment DI
    jmp l3
    
    l2 : mov ah , 0
    mov [di] , al        <<<< The 0 is in AH, use AH instead of AL
    inc di
    jmp l3
    
         
    
    l3 :
    
    mov dl,al            <<<< The 1 or 0 is still in AH, use AH instead of AL
    MOV AH, 02h          <<<< Add 48 to DL before printing
    INT 21H
    cmp si,10            <<<< Could work if (*)
    je l4
    cmp di,10            <<<< Will never work and is redundant
    je l4
    jmp main1
    
    l4 : 
    .EXIT
    END
    

    一个很好的解决方案

    由于输入单个 数字 永远不会产生负数,因此请使用我之前答案中的解决方案。
    下一个代码,就像您的代码一样,从键盘输入一个字符,并将其 ASCII 代码视为您要存储在 x 数组中的字节。对于负字节,您应该输入扩展的 ASCII。使用 Alt 后跟一个不超过 255 的数字。

    ORG 256
     xor  bx, bx
    again:
     mov  ah, 01h      ; DOS.GetCharacter
     int  21h          ; -> AL
     mov  x[bx], al
     mov  ah, 255
     shl  ax, 1
     not  ah
     mov  y[bx], ah    ; 1 for positive, 0 for negative
     inc  bx
     mov  dl, ah
     add  dl, 48
     mov  ah, 02h
     int  21h
     cmp  bx, 10
     jb   again
     mov  ax, 4C00h
     int  21h
    
    x db 10 dup (0)
    y db 10 dup (0)
    END
    

    正确的解决方案

    如果您坚持,您的代码会更正,但遗憾的是没有任何改进:

    .MODEL SMALL
    .DATA
    x db 10 dup ( ? )
    y db 10 dup ( ? )
    .CODE
    mov ax, data
    mov ds, ax
    mov si,offset x  ; De facto SI=0
    mov di,offset y
    main1:
    mov ah,01h
    int 21h
    sub al, 48     ; From character to number [0,9]
    mov [si], al
    inc si
    cmp al,0
    jge l1     ; For positive
    jmp l2     ; For negative
    l1:
     mov ah , 1
    mov [di] , ah
    inc di
    jmp l3
    
    l2 : mov ah , 0
    mov [di] , ah
    inc di
    jmp l3
    
         
    
    l3 :
    
    mov dl,ah
    add dl, 48
    MOV AH, 02h
    INT 21H
    cmp si,10
    je l4
    jmp main1
    
    l4 : 
    .EXIT
    END
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 2020-07-19
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      相关资源
      最近更新 更多