用汇编语言实现:有十个有符号数,按从小到大的顺序排列。 例如:1,-1,2,-2,3,-3,4,-4,5,-5

供稿:hz-xin.com     日期:2025-01-13
51单片机 用汇编语言怎么编程:几个有符号数的比较大小

51单片机中,没有专门的有符号数比较指令,只能通过一些变通的方法来实现,比如:
比较30H和40H中有符号数的大小,结果放在R2中,大于则为1,等于为0,小于为128:
CMPDAT:
MOV A,30H
JB ACC.7,CMPF1
MOV B,A
MOV A,40H
JB ACC.7,CMPZ2 ;30H正,40H负所以30H>40H
CJNE A,B,CMPZ1 ;均为正,比较大小
MOV R2,#0
RET
CMPZ1:
JC,CMPZ2
CMPZ11:
MOV R2,#128
RET
CMPZ2:
MOV R2,#1
RET
CMPF1:
ANL A,#7FH
MOV B,A
MOV A,40H
JNB ACC.7,CMPF3 ;30H负,40H负
SJMP CMPZ11 ;30负,40H正,所以30H<40H
CMPF3:
ANL A,#7FH
CJNE A,B,CMPF10 ;两个均为负数
MOV R2,#0
RET
CMPF10:
JC CMPZ2 ;30H<40H(负数小者大)
SJMP CMPZ11 ;30H>40H

dseg segment a1 dw 1212h,3434h,5656h,7878h,9090h,2121h,4343h,6565h,8787h,9999h c equ ($-a1)/2dseg endsassume cs:cseg, ds:dsegcseg segmentstart: mov ax, dseg mov ds, ax mov cx, c*2 dec cx dec cx$0: mov bx, 0$2: mov ax, a1[bx] cmp ax, a1[bx+2] jl $1 xchg ax,a1[bx+2] mov a1[bx], ax$1: inc bx inc bx cmp bx, cx jl $2 dec cx dec cx jg $0 ;print result mov cx, c mov bx, 0$3: mov ax, a1[bx] call print mov ax,0e20h int 10h inc bx inc bx loop $3 mov ah,4ch int 21hprint proc near push cx push bx xor cx, cx mov bx, 10 test ax, 8000h jz Q0 push ax mov ah,0eh mov al,'-' int 10h pop ax neg axQ0: xor dx, dx div bx xor dx,0e30h push dx inc cx cmp ax, 0 jnz Q0Q1: pop ax int 10h loop Q1 pop bx pop cx retprint endpcseg endsend start

10个有符号数从键盘输入:

        CODE  SEGMENT
              ASSUME    CS:CODE
              ORG       100H
      START:  PUSH      CS
              POP       DS
              PUSH      CS
              POP       ES
              CALL      INPUTNUM
              LEA       SI,@ARRAY
              MOV       CX,@NUM
              CALL      SORT
      
              MOV       AH,2
              MOV       DL,13
              INT       21H
              MOV       DL,10
              INT       21H
      
              MOV       CX,@NUM
              LEA       SI,@ARRAY
              CLD
      @MAIN:
              LODSW
              CALL      DISPAXS
              LOOP      @MAIN
      
              MOV       AH,4CH
              INT       21H

;===============================
; 字数组排序(冒泡排序)
        SORT  PROC      NEAR
      ; 数组长度置入cx,字数组首地址置入 si
              PUSH      AX
              PUSH      CX
              PUSH      DX
              PUSH      SI
              PUSH      DI
              PUSHF
              PUSH      CX
              POP       DX
              DEC       DX
    @SORTL1:
              MOV       CX,DX
              MOV       DI,SI
    @SORTL2:
              MOV       AX,[DI+2]
              CMP       AX,[DI]
              JG        @SORTNEXT
              XCHG      AX,[DI]
              MOV       [DI+2],AX
  @SORTNEXT:
              INC       DI
              INC       DI
              LOOP      @SORTL2
              DEC       DX
              JNZ       @SORTL1
              POPF
              POP       DI
              POP       SI
              POP       DX
              POP       CX
              POP       AX
              RET
        SORT  ENDP
;===============================================
;  键盘输入数值数组子程序(数组为@array,元素个数为@num)
;  可以是负数
    INPUTNUM  PROC      NEAR
              PUSH      AX
              PUSH      BX
              PUSH      CX
              PUSH      DX
              PUSH      SI
              PUSH      DI
              PUSHF
      ; 对数组@array清零
              MOV       CX,100
              MOV       AX,0
              LEA       DI,@ARRAY
              REP       STOSW
              MOV       WORD PTR @NUM,0
      ; 输入的数据以一个空格分隔,以回车符结束输入
              LEA       DX,@STRA
              MOV       AH,10
              INT       21H
              MOV       CL,@STRN
              MOV       CH,0
              MOV       WORD PTR @STRM,CX
              LEA       SI,@STR
              CLD

              LEA       DI,@ARRAY
              MOV       WORD PTR @NUM,0
      @STIN:
              MOV       AX,0
              PUSH      AX
              MOV       BYTE PTR @SIGNN,0
    @AGAIN1:
              CMP       WORD PTR @STRM,0
              JE        @LINE0
              LODSB
              DEC       WORD PTR @STRM
              CMP       AL,' '
              JE        @LINE0
              CMP       AL,'-'
              JNE       @INPUT1
              MOV       BYTE PTR @SIGNN,1
              JMP       @AGAIN1
    @INPUT1:
              SUB       AL,30H
              MOV       AH,0
              MOV       BX,AX
              POP       AX
              MOV       CL,10
              MOV       CH,0
              MUL       CX
              ADD       AX,BX
              PUSH      AX
              JMP       @AGAIN1
     @LINE0:
              POP       AX
              CMP       BYTE PTR @SIGNN,1
              JNE       @INPUT2
              NEG       AX
    @INPUT2:
              MOV       WORD PTR[DI],AX
              INC       WORD PTR @NUM
              CMP       WORD PTR @STRM,0
              JE        @STINEND
              INC       DI
              INC       DI
              JMP       @STIN
   @STINEND:
              POPF
              POP       DI
              POP       SI
              POP       DX
              POP       CX
              POP       BX
              POP       AX
              RET
       @STRA  DB        255
       @STRN  DB        ?
        @STR  DB        255 DUP(?)
       @STRM  DW        0
      @ARRAY  DW        100 DUP(0)
        @NUM  DW        0
       @CHAR  DB        ?
      @SIGNN  DB        0
    INPUTNUM  ENDP

;=========================================
 ; 将要显示的有符号数置于 ax 中
     DISPAXS  PROC      NEAR
              PUSH      AX
              PUSH      BX
              PUSH      CX
              PUSH      DX
              PUSH      SI
              PUSH      DI
              PUSH      BP
              PUSH      DS
              PUSH      ES
              PUSHF
              PUSH      CS
              POP       DS
              PUSH      CS
              POP       ES
              MOV       CX,6
              LEA       DI,DISPAXSS
   @DISPAXS:
              MOV       BYTE PTR [DI],32
              INC       DI
              LOOP      @DISPAXS
              PUSH      AX
              MOV       DL,32
              MOV       AH,2
              INT       21H
              POP       AX
              MOV       BYTE PTR NZS,0
              MOV       BYTE PTR SIGNS,0
              CMP       AX,0
              JGE       @DISPAXS0
              MOV       BYTE PTR SIGNS,1
              NEG       AX
  @DISPAXS0:
              PUSH      AX
              LEA       SI,DIVARRS
              LEA       DI,DISPAXSS
              INC       DI
              MOV       CX,5
  @DISPAXS1:
              POP       AX
              MOV       DX,0
              MOV       BX,[SI]
              DIV       BX
              PUSH      DX
              CMP       AL,0
              JNE       @DISPAXS2
              CMP       BYTE PTR NZS,1
              JE        @DISPAXS2
              CMP       CX,1
              JE        @DISPAXS2
              MOV       DL,20H
              JMP       @DISPAXS3
  @DISPAXS2:
              ADD       AL,30H
              MOV       DL,AL
              MOV       BYTE PTR NZS,1
  @DISPAXS3:
              MOV       BYTE PTR[DI],DL
              INC       SI
              INC       SI
              INC       DI
              LOOP      @DISPAXS1
              POP       DX

              CMP       BYTE PTR SIGNS,1
              JNE       @DISPAXS6
              LEA       SI,DISPAXSS
              ADD       SI,5
  @DISPAXS4:
              CMP       BYTE PTR [SI],32
              JE        @DISPAXS5
              DEC       SI
              JMP       @DISPAXS4
  @DISPAXS5:
              MOV       BYTE PTR [SI],'-'
  @DISPAXS6:
              LEA       DX,DISPAXSS
              MOV       AH,9
              INT       21H
              POPF
              POP       ES
              POP       DS
              POP       BP
              POP       DI
              POP       SI
              POP       DX
              POP       CX
              POP       BX
              POP       AX
              RET
     DIVARRS  DW        10000,1000,100,10,1
         NZS  DB        0
       SIGNS  DB        0
    DISPAXSS  DB        32,32,32,32,32,32,'$'
     DISPAXS  ENDP
;==================================
        CODE  ENDS
              END       START


C语言从键盘输入任意的10个整数,从中找出最大值和最小值并输出_百度知...
代码:include<stdio.h> int main(){ int arr[10]={0};int i=0;int max=0;int min=0;printf("请输入10个整数:");for(i=0;i<sizeof(arr)\/sizeof(arr[0]);i++){ scanf("%d",&arr);} max=arr[0];for(i=0;i<sizeof(arr)\/sizeof(arr[0]);i++){ if(max<arr){ max...

用汇编语言从键盘输入10个数存储后将小于100的输出
4CH INT 21H ;===; 键盘输入数值数组子程序(数组名为@array,元素个数存放在@num中); 可以是负数; 采用字符串缓冲输入,避免原来键入后不能回退修改的问题 ! INPUTNUM PROC NEAR PUSH AX PUSH BX PUSH CX PUSH DX ...

10个单字节有符号数加法,要求数组里面有负数且和要大于FF,数据用原
图中是把二十个字节的数组分成正数组和负数组,并分别计算两个数组中数...

汇编语言:编写将10个无符号二进制数按由小到大的顺序排列的程序,能正确...
;--- SORT PROC ;排序子程序.XOR CX, CX MOV CL, ACTLEN ;数字个数 DEC CX mov si, offset CHARS ;起始地址 s: push cx mov di, si s1: mov al, [di]inc di cmp al, [di]jb jx xchg al, [di]mov [di - 1], al jx:loop s1 pop cx loop...

用汇编语言编写若干个无符号数中的最大值和最小值
调试已通过,解题思路:首先对10个数按从小到大排序,然后第一个元素就是最小的,最后一个元素就是最大的 data segment mes1 db 'the max:$'mes2 db 'the min:$'a dw 42,20,100,456,99,120,89,12334,90,1239 data ends code segment main proc far assume cs:code,ds:data start:push...

51单片机 用汇编语言怎么编程:几个有符号数的比较大小
51单片机中,没有专门的有符号数比较指令,只能通过一些变通的方法来实现,比如:比较30H和40H中有符号数的大小,结果放在R2中,大于则为1,等于为0,小于为128:CMPDAT:MOV A,30H JB ACC.7,CMPF1 MOV B,A MOV A,40H JB ACC.7,CMPZ2 ;30H正,40H负所以30H>40H CJNE A,B,CMPZ1 ;...

汇编语言,设有10个无符号数(8位),试编一程序,求其中所有偶数的和,并将...
stack segment stack byte 256 dup(0)stack endsdata segment C1 dw 0 ;奇数个数 C2 dw 0 ;偶数个数 SUMEVEN dw 0 BUFF db 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ;10个数据的存储缓冲,BUFF[0]存储缓冲区大小,BUFF[1]为需要处理的数据,这里是系统运行...

用有符号数实现排序的汇编语言程序
设有三个字序无符号数存放在buffer缓冲器中,现要对其按从大到小的排列 程序如下:dseg segment buffer db 78,90,11 ;假设的三个无符号数 dseg ends ;cseg segment assume cs:cseg,ds:dseg start: mov ax,dseg mov ds,ax ;mov si,offset buffer mov al,[si]cmp al,[si+1] ;第一个...

无符号数和有符号数在汇编程序里如何区分?
加减运算指令是不区分有符号和无符号的,在计算机对有符号整数的表示只采取一种编码方式,不存在正数用原码,负数用补码的问题。在汇编语言里声明变量的时候,没有signed和unsignde之分,汇编器全部将输入的整数字面量当作有符号数处理成补码存入到计算机中,只有这一个标准,并且全部汇编成补码。也就是说...

汇编语言,设有10个无符号整数(8位),试编一程序,把其中的奇数逻辑左移...
assume cs:code data segment db 1,2,3,4,5,6,7,8,9,10 data ends code segment start:mov ax,data mov ds,ax mov si,0 mov bx,2 mov cx,10 s: mov ah,0 mov al,[si]div bl cmp ah,0 je next ;如果是偶数则进行下一个数 shl ah,1 ;如果是奇数则左移一位 mo...