Small trial Assembly

Source: Internet
Author: User
Tags exit stack pop

Kun's Homework ~

-----------------

;d ate:2014-05-19; author:d4shman@gmail.com; the debugging and running of a cyclic program of lang:assembly experiment One. Experiment objective to master the debugging and operation method of cyclic program two. 
Experimental content 1, program 1; Data segment D1 db 2, -1,-3,5,6,9,18,-29,-72,8,122,-31,95,76,91,-2;d B means the data type after it is byte type (16) RS db? Data ends; stack segment STACK1 segment;  DUP: Repeat defines the initial value specified in parentheses, the number of times is determined by the previous value, here is 100 times; 
: Allocates only storage space, does not specify initial value db (?) Stack1 ends; code snippet segment; In the assembly, far and near are the parameters of the subroutine invocation, and if the subroutine and the caller are in one segment, the subroutine parameter is set to next, otherwise far; This is set to far because the system considers the main program as a DOS call.                     subroutines, and they're not in the same paragraph. main proc far;
    Assume: Segment Register Association description pseudo-directive; Cs:code, Ds:data, Ss:stack1: Code segment Register CS is associated with code, data segment register DS is associated with a, and the stack segment register SS is associated with Stack1. Assume Cs:code,ds:data,ss:stack1; Defines the program entry Start:push DS, presses the data segment register DS into the stack sub Ax,ax, and the AX register 0 (other methods include: 1.MOV ax,0 2.AND ax,0 3.XOR ax,ax) push ax; Ax register into the stack mov ax,data; data is staged to AX MOV ds first , Ax, and then to the DS Register by AX (after these two steps because the assume pseudo-directive is only "associative", not "assigned") Lea BX,D1; The data in the D1 is stored in the register BX in the Mov cx,16; CX StorageThe value is set to 0 MOV dl,0; the value of the register DL stored is set to AL,[BX], and the value in the register bx is stored in the Lop1:mov of the CMP al,0; compare the value in register AL With a size of 0 jge jus; If the value in Al is greater than or equal to 0, jump to jus inc DL; otherwise, the value in the register DL is 1 jus:inc bx; Register BX The value of 1 Dec CX, the value of register CX minus 1 jnz lop1; If the value in CX is not 0, transfer to the loop body LOP1 mov rs,dl; value in the DL Deposit RS Add dl,30h;d l stored value plus 30h mov ah,2; ah has a value of 2 int 21h; int 21h is the interrupt instruction in the assembly, the control Make the right to DOS RET; return to main ENDP code ends; end of program flag end start comment*///////////////+ summary +/////////////
   The assembly code above implements the following C program (the number of negative numbers in the statistics array): int a[16] = {2, -1,-3,5,6,9,18,-29,-72,8,122,-31,95,76,91,-2};
   int d = 0;
   int c = 16;
		while (c) {if (A[i] >= 0) {;//do nothing} else {d++;
    } c--;
} return D; ---------///////////////////* 2. Program 2 data segment num DW 6789h; define variable num is a double-byte (dw:double word) type with a value of 6789h data ends Stack1 SEGMENT db-dup (?) stack1 ends Code segment main proc far assume Cs:code,ds:data,ss:stack1 Start:pu SH ds sub Ax,ax push AX mov ax,data mov ds,ax; the above and program 1 are the same MOV b X,num; the value of num is stored in the DS MOV ch,4; the value in CH is 4 Rota:mov cl,4; The value in CL is 4 rol bx,cl; Move the high 4 bits in the BX
          To a low 4 bits, such as 1010 1111 0110 0000 to 1111 0000 0110 1010 MOV al,bl; Save the value in the BX lower address (bl:b low) to Al, which is 0110 1010 and al,0fh 0110 1010 and 0000 1111 are retained only after 4bit add al,30h; The value in Al is added 30h CMP al,3ah; compare AL The value and the size of the decimal number 9 JL print; When the value in the AL is less than 3ah, transfer to the Print function add al,07h; otherwise (a-f), add the value of Al to 07h (ASCII code between 9 and a 7 Number) Print:mov Dl,al; Save the value in Al to the DL mov ah,2 int 21h; When an interrupt occurs, the value stored in AH is 2, which represents the 2nd function-character output, and 
    The character is stored in the DL data Dec CH, the value in ch minus 1 jnz rota; When the value in CH is not 0 o'clock, transfer to rota ret main ENDP
         Code endsEnd start comment*///////////////+ summary +///////////////////The above assembly code implements the 16-in-turn 2-in-binary function. reference:1), http://222.240.219.68:8080/files/files_upload/content/material_161/COLUMN_4/default.htm 2),/HTTP www.qi9.cn/html/785_443_8.html///////////////--------///////////////////* Experiment two PC Serial interface experiment one. Experiment objective to master the basic serial communication of PC two.
                The experimental content reference program data segment flag DB 0; Defines the variable flag, type Byte, value 0 info db 13,10, 13 is carriage return, 10 is newline (see ASCII code table) DB ' Please select (s=sending or r=receiving) ' db ' $ '; '
                         $ ' indicates that it has reached the end of the string Sendinfo DB 13,10 db ' sending ... ' db 13,10
                          DB ' $ ' receiveinfo db 13,10 db ' receiving ... ' DB 13,10 db ' $ ' data ends stack segment para stack ' stack '; if there's no last one ' s Tack ' Description, you will need to set your own SS:SP db (0), initialize the 256-byte space, and use 0 to populate the stack ends code SEGMEnt start proc far assume Cs:code,ds:data; because of the ' stack ' above, there is no need to set the push DS;d s into the stack mov ax,0; ax clear 0 push ax; ax into the stack mov ax,data mov ds,  AX;d ATA data into DS mov al,0h; Al clear 0 out 21h,al; Outputs 0 data from Port 21H mov The value in DX,2FBH;d x is 2FBH mov al,80h; the value in Al is 80h out dx,al; The data in Al is output from the 2FBH port (below the duplicate, not
                talk) MOV dx,2f8h mov al,060h out dx,al mov dx,2f9h 
                mov al,0 out dx,al mov dx,2fbh mov al,0ah out dx,al
                MOV dx,2fch mov al,0ah out dx,al mov dx,2fch
                mov al,0bh out dx,al mov dx,2f9h mov al,01h out dx,al					STi	; CLI is off interrupt, prevent some hardware interruption to the program interference, STI is open interrupt, allow hardware interrupt; Push DS mov Dx,offset irecve; Assigns the offset address of the IRECVE in the paragraph to the DX MOV ax,seg irecve; seg is a pseudo-operator used to take the back symbol Irec ve segment address mov ds,ax mov al,0bh mov ah,25h int 21h; Call DOS Number 25h features pop DS mov dx,offset info mov ah,9 int 21h; call DOS 09h function mov ah,1 int 21h; call dos 01h function cmp al, ' s '; compare values and characters in AL S ' JE sending; if same, transfer to sending CMP al, ' s ' je sending C MP Al, ' s '; compare the values in Al and the characters ' s ' jne next; when they are not, transfer to next Sending:call send; call s
                End Function ret next:cmp al, ' R '; compare the values in Al and the characters ' r ' JE receiving; if the same, move to receiving CMP al, ' R '; compare jne quit; if different, transferto quit Receiving:call receive; Call the receive function Quit:ret start ENDP; Implement message receive function rec eive proc;p Roc is a pseudo-directive that defines a subroutine, and ENDP respectively indicates that the start and end of the subroutine definition must appear in pairs of mov dx,offset receiveinfo; the above appears, the offset of receiveinfo in the paragraph					Address assigned to DX mov ah,9 int 21h; system call number 9th REFORE:CMP flag, '! '
        ; compare flag and character '! ' JZ sequit; If same, transfer to Sequit jmp refore Requit:ret receive ENDP; end of subroutine; Implement message sending function send proc; another child. Program mov dx, offset sendinfo;(here and above receive subroutine like, not talk) mov ah,9 int 21h sefore:cmp flag, '! ' JZ sequ It; if flag is!, then transfer to Sequit mov dx,2fdh in AL,DX; Read 8 bits from the 2FDH port, put in the AL Register Test al,20h; test: Two operands and operation, only change the flag bit , does not echo the result JZ sefore; test result is zero, then transfer to sefore mov ah,0 int 16h; Keyboard Service, waiting for the keyboard input MOV Flag,al mov dx,2f8h out dx,al; the data from the 2f8h port will be placed in the output MOV bx,0 mov ah,14 int 10h; Display character at current cursor, reference:http://www.itzhai.com/assembly-int-10h-description.html jmp SefoRe Sequit:ret send ENDP; subroutine end IRECVE proc; subroutine 3 push ax push bx push DX p Ush DS mov dx,2fdh in al, DX test al,1eh JNZ error; Test results not transferred to error mov dx,2 0 o'clock  f8h in AL,DX mov flag,al and AL,7FH mov bx,0 mov ah,14 int 10h jmp
        Exit Error:mov dx,2f8h in AL,DX; Read 8 bits from 2f8h port, put in Al Register mov al, '? '
        MOV bx,0 mov ah,14 int 10h Exit:mov al,20h out 20h,al pop ds;d s off stack Pop dx pop bx pop ax STI iret; return irecve ENDP from interrupt; subroutine end code ends en D start comment*///////////////+ summary +///////////////////The above assembly code implements the message sending and receiving function///////////////--------///// ////////////// 
*


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.