Basic Assembly knowledge input and output

Source: Internet
Author: User
I have been studying assembler recently, but the input and output are my first difficulties. Here I will briefly describe some basic output and output strings, characters, output numbers, and other knowledge of assembler, it is only for everyone to learn. This is only some basic knowledge. Compilation experts can also learn some basic input and output knowledge. 1. The output string below is the code that calls the 9 function to output the string "Hello World.
Datas segment; Output string 13 press enter 10 line feed $ end sign (\ 0) inputdb13, 10, 'Hello word! ', 13, 10,' $ 'datas endscodes segment assume Cs: Codes, DS: datasstart: mov ax, datas mov ds, ax; data is placed into Ds Lea dx, input; string offset start dx, same as mov dx, offset ASC command mov ah, 9; 09 function print string int 21 h; int 21h is the interrupt Number of DOS interrupt function call, call mov ah, 4ch; return DOS system int 21 hcodes ends end start
Output result:

2. The following code calls the 2-character function to output a single character "a" and calls the 1-character function to input a single character.
Datas segment; enter the data segment code inputdb13, 10, 'Please input: ', 13, 10,' $ 'datas endscodes segment assume Cs: Codes, DS: datasstart: mov ax, datas mov ds, ax; data is stored in DS mov DL, 'a'; characters are stored in registers mov ah, 02 h; a single character int 21 H is output by calling the 2 function; int 21h call function; call the 9 function output string "enter" Lea dx, input mov ah, 9 int 21 h mov ah, 01 h; call the "1" function to enter only one character. Int 21 h mov ah, 4ch. Return the DOS system int 21 hcodes ends end start.
Output result:

3. Output 4 digits a problem is always encountered when doing a compilation experiment. A number such as 2030 is not output. (the maximum 8 digits is 5 digits)
Below is the code for outputting a string of numbers. The method used here is (it must be a 4-digit number)
1. Divide by 1000.2030/1000 = 2 (suppliers)... 30 (remainder) to determine whether the suppliers are 0 and not 0;
2. After the output parameter is set to bits, the operator assigns a value to the register and then jumps to the Unit to judge the number of bits;
3. the key here is to set a judgment variable nozero, if it is 3/1000 = 0 (quotient ).. 3 (remainder). If it is 0, it determines whether the variable is 0. If it is 0, it means it is smaller than 1000, and no 0 is output. Otherwise, 0003 is output. when the first digit of a number is not 0 (for example, the first digit of 2030 is 2, but not 0), the variable nozero is 1, and the subsequent 0 is displayed in the number. (For example, 2000. When the output is 2, nozero = 1, the remainder is 0, and the subsequent 0 are all output. however, 30 values are assigned to 1 only to ten nozero values, and the output value is 0 ).
4. Judge ten places in sequence.
Datas segment; the variables A and B are unsigned integers 0-9999 adw2030; the variables a num DW? Nozero DW?; Add a judgment flag variable datas endscodes segment assume Cs: Codes, DS: datasstart:; ------------------------------------------------------------------------- mov ax, datas mov ds, ax; 16-bit departure in DX: in an ax pair of registers, mov ax, amov nozero, 0; like a Boolean variable; random; outputs a thousand output0:; Outputs A to store XOR dx, dxmov num, 1000div nummov BX, ax; axmov CX, DX; remainder dxcmp BX, 0 JNE outp Utnum0; vendor AX =! 0: Number output; key: add this judgment to prevent 0 from outputting CMP nozero, 0; variable nozero! = 0 output number 0jne outputnum0; equivalent to 0003 first 3 0 filter 2000 last 3 0 display; bytes; output-bit output1: mov ax, CX; DX: ax remainder CX is assigned to axxor dx, dxmov num, 100div nummov BX, ax; axmov CX, DX; remainder dxcmp BX, 0jne outputnum1 CMP nozero, 0jne outputnum1; struct ;-------------------------------------------------------------------------; output 10 output2: mov ax, CX; DX: ax CX values to axxor dx, dxmov num, 10div nummov BX, ax; axmov CX, DX; remainder dxcmp BX, 0jne outputnum2 CMP nozero, 0jne outputnum2; random; output single-digit output3: mov ax, CX; DX: ax CX assigned to axxor dx, dxmov num, 10div num add dx, 30 h; remainder dxmov ah, 2int 21 h; exit program JMP exit; export outputnum0:; program output number mov nozero, 1; flag variable value 1add BX, 30 h; add a number of 30 h to the ASC code number mov dx, BX; assign a value to DX to call the interrupt, otherwise the mov ah, 2 will be reported; call the 2 function to output the character int 21 hjmp output1; extends outputnum1:; the program outputs the numbers mov nozero, 1; the variable value is 1add BX, 30 h; the number plus 30 h is the ASC code number mov dx, BX; the value DX must be assigned to call the interrupt. Otherwise, the mov ah, 2 will be reported. The output character int 21 hjmp output2; then outputnum2:; the program outputs the number mov nozero, 1; the value of the Flag variable is 1add BX, 30 h; the value of the number plus 30 h is the ASC code number mov dx, BX; the value must be dx to call the interrupt; otherwise, the mov ah, 2 error will be reported; call function 2 to output the character int 21 hjmp output3; exit Exit:; program end template: Use ah to select the function. The 4ch function allows you to exit the program mov ah, 4ch int 21 h; ------------------------------------------------------------------------- codes ends end start

 

The output result is: when a is assigned 3, the corresponding 3 instead of 0003 is output.

This method seems clumsy. Every time the output number is written with repeated output code four times and jumps back and forth, but more importantly, it provides a nozero idea, methods Commonly Used to set bool variables in C, C ++, C #, and Java are also useful and useful in Assembler programs. in the experiment, I also used the following definition subroutine method to output numbers. 4. using the function to output four digits below is a method that Baidu Knows. Calling the function to output digits has not yet learned subfunctions, so this method is not verified here, however, this method is quite useful. the above nozero is also based on its idea. it is for everyone to learn.
Web: http://zhidao.baidu.com/question/54461195.html
Outd proc push CX mov nozero, 0; like a Boolean variable mov CX, 10000 call decdiv mov CX, 1000 call decdiv mov CX, 100 call decdiv mov CX, 10 call decdiv mov CX, 1 call decdiv; CMP nozero, 0; JNE outdexit; MoV DL, 30 h; MoV ah, 2; int 21 houtdexit: Pop CX ret; call: Incoming in BX, the divisor is input in CX; RET: printer; the remainder is sent to bxdecdiv proc mov ax, BX mov dx, 0 Div CX mov BX, DX mov DL, Al cmp dl, 0 JNE dispdigit CMP nozero, 0 JNE dispdigit JMP decdivexitdispdigit: mov nozero, 1 add DL, 30 h mov ah, 2 int 21 hdecdivexit: retdecdiv endpoutd endp
5. conclusion: The above article mainly serves as a reference for students who have just learned the compilation. at the same time, the Code is not very optimized and redundant, but it still provides some good Assembly ideas for everyone to learn. I hope everyone can learn some ideas. the above four codes are very useful when I just learned assembly.

 

 

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.