汇编语言程序设计

2025-02-24 10:47:16
推荐回答(1个)
回答1:

dseg segment
  q dw 500 dup (0) ;max input 500
  l dw 0 ;input's length
  msg db "Input n's decimal number 0-65535:$"
  msg1 db '^ 2',0dh,0ah,'$'
dseg ends

cseg segment
assume cs:cseg, ds:dseg
start:
  mov ax, dseg
  mov ds, ax

  mov ah,9
  lea dx, msg
  int 21h

  lea si, q
  call readn

  mov ax, 0e0dh
  int 10h
  mov al,0ah
  int 10h

  mov cx, l
  lea si, q
p0:
  push cx
  call issquare
  cmp bx, 1
  jnz p1
  mov ax, [si]
  call print
  mov ax,0e3dh ;print =
  int 10h
  mov ax, cx
  call print
  mov ah,9
  lea dx, msg1
  int 21h
p1:
  add si, 2
  pop cx
  loop p0

  mov ax, 4c00h
  int 21h
readn proc near
  mov cx, 0
  mov bx, 10
r0:
  mov ah,7
  int 21h

  cmp al,0dh
  jz r2
  cmp al,20h
  jz r1
  cmp al,'0'
  jb r0
  cmp al,'9'
  ja r0
  mov ah,0eh
  int 10h

  and ax,0fh
  xchg ax, [si]
  mul bx
  add [si], ax
  mov cx, 1
  jmp r0
r1:
  inc l
  mov cx, 0
  add si, 2
  mov ah,0eh
  int 10h
  jmp r0

r2:
  cmp cx, 1
  jnz r3
  inc l
r3:
  ret
readn endp

print proc near
  push cx
  mov bx, 10
  mov cx, 0
q0:
  xor dx, dx
  div bx
  xor dx, 0e30h
  push dx
  inc cx
  cmp ax, 0
  jnz q0
q1:
  pop ax
  int 10h
  loop q1

  mov al,20h
  int 10h

  pop cx
  ret
print endp

;parameter=[si]
;ret bx=0: false, bx=1:true
issquare proc near
  mov ax, [si]
  mov bx, 1
  mov cx, 0
i0:
  sub ax, bx
  jc  i1
  add bx, 2
  inc cx
  jmp i0
i1:
  mov bx, 0
  mov ax, cx
  mul cx
  cmp ax, [si]
  jnz i2
  mov bx, 1
i2: 
  ret
issquare endp

cseg ends
end start