1 / 16

Input And Output (I/O) In Assembly Language

Input And Output (I/O) In Assembly Language. As mentioned before , input and output (I/O) in assembly language can be quite difficult and complicated .

meriel
Download Presentation

Input And Output (I/O) In Assembly Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Input And Output (I/O) In Assembly Language • As mentioned before, input and output (I/O) in assembly language can be quite difficult and complicated. • To that end, it is possible to access the input/outputcapabilities that are available in the C programming language, and of the various high-levellanguages that MASM can interface with, C is probably the easiest.

  2. Input And Output (I/O) In Assembly Language • When learning a new programming language, one of the first programs learned is the infamous “Hello World” program. • The advantage of such a program is to insure that theprogram has compiled or assembled correctly and subsequently executed properly. Thisprogram in C often appears as follows: #include <stdio.h> int main(){ printf("Hello World!\n"); return 0; } • where printf is the method used for output, the string to be output is in double quotes,and the \n means advance to the next line, similar to using endl in a cout statement inC++ and similar to the ln portion of a system.out.println() statement in Java.

  3. Input And Output (I/O) In Assembly Language • The corresponding program to output “Hello World” in MASM would appear as follows: .386 .model flat, c .stack 100h printf PROTO arg1:Ptr Byte .data msg1 byte "Hello World!",0Ah,0 .code main proc INVOKE printf, ADDR msg1 ret main endp end • The PROTO directive, preceded by the label printf, indicates a prototype for the functionprintf. When the assembler encounters the printf, it does not cause an error butrather leaves space for the address of the instruction to be filled in later by the linker priorto being loaded into memory for execution. The parameter arg1:Ptr Byte indicatesthat the argument of the printf will be a pointer to a string of bytes.

  4. Input And Output (I/O) In Assembly Language • In order to call the printf function, the INVOKE directive is used, which is likecalling a subprogram, but is simpler to use because it takes care of theparameter passing. However, be very careful to note that the INVOKE directive destroysthe contents of the eax, ecx, and edx registers. Again as mentioned before, it iswise to save data in memory locations instead of leaving them in registers to avoid thepossibility of long debugging sessions. • Continuing, the argument ADDR msg1 in the INVOKE above indicates the addressof the string to be output. The actual message to be output is in the .data section asmsg1 byte "Hello World",0Ah,0, • The difference here is that the string is followed by a 0Ah, which is the hexadecimal codefor a new line, such as \n in C. The 0Ahis followed by a 0, which is the code to terminate a string used with output.

  5. Input And Output (I/O) In Assembly Language • The above code is good for outputting a single character string, but what if there isa need to format and output a number of parameters? As a transition step to the abilityto output more than one argument, the original C program above could be rewritten as follows: #include <stdio.h> int main(){ printf("%s\n","Hello World!"); return 0; } • The %s indicates that there is a string in the first argument followingthe current formatting argument. Although in C the formatting and data are often together,their separation makes for a little cleaner code in assembly language when there is morethan one item to be output.

  6. Input And Output (I/O) In Assembly Language .386 .model flat, c .stack 100h printf PROTO arg1:Ptr Byte, printlist:VARARG .data msg1fmt byte "%s",0Ah,0 msg1 byte "Hello World!",0 .code main proc INVOKE printf, ADDR msg1fmt, ADDR msg1 ret main endp End • First note that the PROTO statement has an additional argument printlist:VARARG, which indicates that a variable number of arguments can now follow the firstargument, where the first argument will now serve as the format string. In the data declarationsection, note that the %s is in a separate data declaration called msg1fmt, where %sindicates that string data will be output. Also, the string to be output is only terminated bythe 0 string terminator and the 0Ah has been moved to msg1fmt. Lastly, the first ADDR inthe INVOKE directive references the format string and the second one references the string to be output.

  7. Input And Output (I/O) In Assembly Language • In addition to outputting a single string, the previous example can be expanded to outputmultiple strings. Further, it can be expanded to output multiple integers or a combinationof strings and integers. The advantage of this is that the integer output can be identified tothe users with matching strings. For example, in the following C program, the integer 5 isoutput along with an identifying string: #include <stdio.h> int main(){ int number; number = 5; printf("%s%d\n","The number is: ",number); return 0; }

  8. Input And Output (I/O) In Assembly Language • The first argument of the printf says that a string will be output (%s), followed byan integer (%d), followed by a line feed. The second argument of the printf is the stringand the third is the variable number. The corresponding MASM code is as follows: .386 .model flat, c .stack 100h printf PROTO arg1:Ptr Byte, printlist:VARARG .data msg1fmt byte "%s%d",0Ah,0 msg1 byte "The number is: ",0 number sdword ? .code main proc mov number,5 INVOKE printf, ADDR msg1fmt, ADDR msg1, number ret main endp end

  9. Input And Output (I/O) In Assembly Language • As in the last example of the previous section, the PROTO statement remains unchanged.Note that the msg1fmt string has the %d added to it. The variable number has beendeclared as a signed double word in the data section and the number 5 assigned to it in thecode segment. Lastly, the variable number has been added as an argument to the INVOKEdirective. Both msg1fmt and msg1 need ADDR because they are pointers to the strings,but ADDR is not needed for number because it is a simple integer variable.

  10. Input And Output (I/O) In Assembly Language • The following example further illustrates how multiple arguments work and includestwo integers in addition to a string. It also includes cleaner output by including bettervertical spacing by using \n and better horizontal spacing by using spaces in the string as shown below: #include <stdio.h> int main(){ int num1 = 5, num2 = 7; printf("\n%d%s%d\n\n",num1," is not equal to ",num2); return 0; } The above C program would be implemented in assembly as follows: .386 .model flat, c .stack 100h printf PROTO arg1:Ptr Byte, printlist:VARARG .data msg1fmt byte 0Ah,"%d%s%d",0Ah,0Ah,0 msg1 byte " is not equal to ",0 num1 sdword 5 num2 sdword 7 .code main proc INVOKE printf, ADDR msg1fmt, num1, ADDR msg1, num2 ret main endp • Without any change to the PROTO directive in the above program, there are now threearguments after the msg1fmt string in the INVOKE directive. As mentioned previously,the reason extra arguments are allowed is due to the VARARG in the PROTO directive whichallows for a variable number of arguments. Again, notice that 0Ah is used instead of \nand the careful use of spaces in the string, both to assist in the vertical and horizontal spacing.

  11. Input And Output (I/O) In Assembly Language • Although having the ability to output is extremely important, it does lead to some dullprograms unless one can also input data. Just as printf can be invoked to allow foroutput, so too can scanf be invoked for input. Instead ofmerely assigning an integer to thevariable number, the following C program inputs a number from the user and then outputs the same number #include <stdio.h> int main(){ int number; scanf("%d",&number); printf("\n%s%d\n\n","The number is: ",number); return 0; } • Notice in the above code that number is preceded with an ampersand (&) in thescanfbut not in the printf. The ampersand indicates the address of number is being passed to scanf to allow the valueread in from the keyboard to be passed back to the variable number.Whereas with output,the value in number being passed to printf will be output and since no number will bepassed back, an ampersand is not needed.

  12. Input And Output (I/O) In Assembly Language • The following assembly program implements the above C program: .386 .model flat, c .stack 100h printf PROTO arg1:Ptr Byte, printlist:VARARG scanf PROTO arg2:Ptr Byte, inputlist:VARARG .data in1fmt byte "%d",0 msg1fmt byte 0Ah,"%s%d",0Ah,0Ah,0 msg1 byte "The number is: ",0 number sdword ? .code main proc INVOKE scanf, ADDR in1fmt, ADDR number INVOKE printf, ADDR msg1fmt, ADDR msg1, number ret main endp

  13. Input And Output (I/O) In Assembly Language • Although there are a number of similarities between the scanf and printf above,such as the similarity between the two prototypes, there are some important details thatneed to be pointed out. • First, note that a %s does appear in the input format, because onlyan integer is being input in this example. Further, the input format is terminated only bya 0 and does not contain a 0Ah. The reason is that during input a new line is not neededbecause it is supplied by the user after the data has been entered and they press the “enter”or “return” key, which supplies the new line on the screen. Lastly, notice that the variablenumber is preceded by ADDR in the invoking of scanf, but it is not preceded by ADDR inthe printf. The reason for this is that ADDR serves the same function as the ampersand(&) in C as discussed above.

  14. Input And Output (I/O) In Assembly Language • The reason is that when either the above C or the MASM program executes, there is just a cursor blinkingon the screen and no indication to the user that any input is needed or what type of inputis needed. Instead, as with any language, it is a good idea to prompt the user for the typeof input needed as shown in the C program below, where the prompt and output messagehave been changed to specify an integer instead of just a generic number: #include <stdio.h> int main(){ int number; printf("\n%s","Enter an integer: "); scanf("%d",&number); printf("\n%s%d\n\n","The integer is: ",number); return

  15. The corresponding assembly code is given as follows: .386 .model flat, c .stack 100h printf PROTO arg1:Ptr Byte, printlist:VARARG scanf PROTO arg2:Ptr Byte, inputlist:VARARG .data in1fmt byte "%d",0 msg0fmt byte 0Ah,"%s",0 msg1fmt byte 0Ah,"%s%d",0Ah,0Ah,0 msg0 byte "Enter an integer: ",0 msg1 byte "The integer is: ",0 number sdword ? .code main proc INVOKE printf, ADDR msg0fmt, ADDR msg0 INVOKE scanf, ADDR in1fmt, ADDR number INVOKE printf, ADDR msg1fmt, ADDR msg1, number ret main endp end

More Related