Hello world advanced assembler series [transferred from KingofCoders]

Source: Internet
Author: User

Hello world advanced assembler Series

Title *** Hello, world advanced program selection branch by lluct ***

Data Segment; define data segments
Msg1 dB '*** welcome to my program by lluct ***', '$'
; Defines the first string of the output. The string must be defined in dB, and $ indicates the end mark.
Msg2 dB '1: basic message 2: Advanced message', '$'
; Define output string information: Select menu
Msg3 dB 'Please choose: ',' $'
; Define the output string information: select the prefix
Msg4 dB 'hello, world! ^-^ ',' $'
Define output string information: branch 1 Information
Msg5 dB 'This is my first asm_86 program! @ ^-^ @ ',' $'
Define output string information: branch 2 Information
Errmsg dB 'Choose error! -_-B ',' $'
; Define output string information: Select error information
Data ends; end of Data Segment

Code segment; defines the code segment
Assume Cs: Code; specifies the content of CS
Assume DS: data; specifies DS content

Start: mov ax, data; the program starts from start
MoV ds, ax; DS set the initial value, data segment address

Call enter; call the subprogram that displays line breaks
Lea dx, msg1; returns the offset address of the first string
Call dispchs; call the display string subroutine
Call enter; call the subprogram that displays line breaks
Call enter; this... same as above ^-^

Lea dx, msg2; returns the offset address of the second string
Call dispchs; call the display string subroutine

Again: Call enter; defines the again label. Used to select an error Loop

Lea dx, msg3; returns the offset address of the third string
Call dispchs; call the display string subroutine

MoV ah, 01 H; call the 1 function: enter a character from the keyboard and echo
Int 21 h; input echo complete
CMP Al, '1'; The entered characters are compared with 1
Je basicp; if equal, transfer to basicp label (JE = jump if equal)
CMP Al, '2'; the input character is compared with 2 |
Je advanp; if equal, transfer to the advanp label (JE = if equal, transfer)
JMP error; otherwise, it is unconditionally transferred to the error number.

Exit: mov ah, 4ch; 4C function call: Terminate the current program and return the calling program
Int 21 h; returns DoS

Basicp: Call enter; what should I explain? Dizzy -_-!!!
Lea dx, msg4; returns the offset address of the third string
Call dispchs; call the display string subroutine
Call enter ;..........
Jmp exit; unconditionally transferred to the EXIT label

ADVANP: call enter; 55555555
Lea dx, MSG5; I explained it four times. You should understand it.
Call dispchs; CALL the display string subroutine
CALL ENTER
Jmp exit; unconditionally transferred to the EXIT label

ERROR: CALL ENTER
Lea dx, ERRMSG; Output selection error message
Call dispchs; CALL the display string subroutine
Mov dl, 07 H; Alarm (Bell) controller for output ASCII code BEL (07 H)
Call dispch; CALL to display a single character subroutine
CALL ENTER
JMP AGAIN

DISPCH PROC NEAR
A single character subroutine is displayed. NEAR indicates that the subroutine and the main program are in the same code segment (currently no main program is called)
Mov ah, 02 H; 2 function call: Display output character
INT 21 H; finished output display
RET; Return
Dispch endp; subprogram end

Enter proc near; display the carriage return Subprogram
Mov dl, 0DH; carriage return controller for output ASCII code CR (0DH)
Call dispch; CALL to display a single character subroutine
Mov dl, 0AH; line feed controller for output ASCII code LF (0AH)
Call dispch; CALL to display a single character subroutine
RET; Return
Enter endp; end of subroutine

Dispchs proc near
The string subroutine is displayed. Near indicates that the subroutine and the main program are in the same code segment (currently no main program is called)
MoV ah, 09 h; 9 function call: Display string
Int 21 h; finished output display
RET
Dispchs endp

Code ends; end of code snippet
End start; End Assembly

Copy the above Code to a text program such as Notepad and save it. (For example, helloch. ASM)
; Compilation: MASM helloch. ASM
; Connection: link helloch. OBJ
; Execute: helloch.exe

========================================================== ====================

Title *** Hello, world advanced string input and output by lluct ***

Data Segment; define data segments
Input dB 100 DUP (?)
Define the input string. The string must be defined in dB and the length is 100 bytes.
Msg1 dB 'hello, ',' $'
; Defines the output prefix string information. The string must be defined in dB, $ is the end sign (24 h)
Msg2 db', welcome to here! ',' $'
; Define output suffix string Information
Headmsg dB 'Please input your name: ',' $'
; Start to display string Information
Data ends; end of data Segment

Code segment; defines the code segment
Assume cs: code; specifies the content of cs
Assume ds: data; specifies ds content

Start: mov ax, data; the program starts from start
Mov ds, ax; ds set the initial value, data segment address
Mov si, 0; Initial Value of the address change register

Call enter; call the subprogram that displays line breaks
Lea dx, headmsg; offset address of the string to be output
Call dispchs; call the display string subroutine
Repeat: mov ah, 01 h
Defines the repeat label, which is used to repeatedly enter a single character. Call the 1 function: enter a character from the keyboard and echo
Int 21 h; input echo complete
Cmp al, 0dh; input character and CR (Press ENTER) Comparison
Je exit; if it is equal to press enter, it will be transferred to exit
Mov input [si], al; transfers the value of al to the si address of input (as if so)
Inc si; si plus 1
Jmp repeat; unconditionally transferred to repeat

Exit: call enter
Mov input [si], 24 h; end sign ($) is added to the input string)
Call enter
Lea dx, msg1; offset address of the output prefix string
Call dispchs; call the display string subroutine
Lea dx, input; output the string just entered
Call dispchs
Lea dx, msg2
Call dispchs
Call enter

MoV ah, 4ch; 4C function call: Terminate the current program and return the calling program
Int 21 h; returns DoS

Enter proc near; display the carriage return Subprogram
MoV DL, 0dh; carriage return controller for output ASCII code Cr (0dh)
Call dispch
MoV DL, 0ah; line feed controller for output ASCII code LF (0ah)
Call dispch
RET; Return
Enter endp; subroutine ends

Dispch proc near
MoV ah, 02 h; 2 function call: Display output character
Int 21 h; finished output display
RET; Return
Dispch endp

Dispchs proc near
MoV ah, 09 h; 9 function call: Display string
Int 21 h; finished output display
RET; Return
Dispchs endp

Code ends; end of code snippet
End start; End Assembly

Copy the above Code to a text program such as Notepad and save it (such as heinout. C)
; Compilation: MASM heinout. ASM
; Connection: link heinout. OBJ
; Run: heinout.exe

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.