Win32 compilation [3]: Several output methods in the console

Source: Internet
Author: User
Tags win32 window microsoft c
In the process of familiarizing yourself with commands, pseudo commands, and assembly syntax, the first thing you need is the means of output display.

I used console output when I first started learning, and finally found that it is better to use debug output for Win32 compilation.

The following are several methods of console output:
1. Use the stdout function provided by MASM;
2. Use System APIs:
3. Use the printf function in msvcrt. dll of Microsoft C standard library.

Use the stdout function of MASM:

 
; Test3_1.asm; TestCodeCreate a console project: file-> new project-> console app... 386. Model flat, stdcallinclude masm32.incinclude extends masm32.libincludelib kernel32.lib. Data sztext DB "Hello world! ", 0. codestart: invoke stdout, ADDR sztext ret; RET is used for sub-ProgramThe returned command, which is used to replace exitprocess (this is acceptable when the Win32 window is not generated) end start

  

Use System API functions:

; Test3_2.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incincludelib kernel32.lib. Data sztext dB 'Hello world! ', 0; defines two DWORD type variables, which are used for output handle and string length. Data? Hout dd? Len dd ?. Codestart:; get the handle of the output device on the console. The returned values will be placed in the eax register invoke getstdhandle, std_output_handle; give the obtained handle to the variable Hout mov Hout and eax; the lstrlen function is used to obtain the string length. The returned values are eax invoke lstrlen and ADDR sztext. The obtained string length is given to the variables Len mov Len and eax. The parameters are output to the console: handle, string address, and string length. The following two pointers do not currently use invoke writefile, Hout, ADDR sztext, Len, null, null retend start; in addition, the previously used stdout is basically implemented in this way. The source code is: masm32 \ m32lib \ stdout. ASM

  

Use the printf function in the Microsoft C standard library; msvscrt. inc declares it as crt_printf

; Test3_3.asm.386.model flat, stdcallinclude msvcrt. incincludelib msvcrt. Lib. Data sztext dB 'Hello world! ', 0. codestart: invoke crt_printf, ADDR sztext retend start

  

C Function crt_printf should be recommended in comparison to the three methods, because it can easily output more data types, such:

 
; Test3_4.asm.386.model flat, stdcallinclude msvcrt. incincludelib msvcrt. lib. data szfmt dB 'eax = % d; ECx = % d; edX = % d', 0. codestart: mov eax, 11 mov ECx, 22 mov edX, 33 invoke crt_printf, ADDR szfmt, eax, ECx, EDX retend start

  

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.