SoFunction
Updated on 2025-03-04

Use of basic assembly code for UEFI development

Use assembly code in UEFI

The EDK code contains a part of the assembly code. Currently, the .S, .asm and .nasm format assembly (the first is AT&T assembly, and the last two are Intel assembly, but the assembly styles used are slightly different. .nasm is open source and free, and is more general). If it is compiled under Windows, it is generally used to use the NASM compiler, so the assembly file in the .nasm format is used, and the compilation tool is also free, so you can use it in/Download and put it in the C:\Nasm directory, and then it can be used in the EDK code.

Here is an example, which is a library module:

First create the inf file:

[Defines]
  INF_VERSION                       = 0x00010005
  BASE_NAME                         = AsmLib
  FILE_GUID                         = 2A3061AF-740E-4B62-B900-FC24AF9B072E
  MODULE_TYPE                       = BASE
  VERSION_STRING                    = 1.0
  LIBRARY_CLASS                     = AsmLib
#
# The following information is for reference only and not required by the build tools.
#
#  VALID_ARCHITECTURES              = IA32 X64 IPF EBC
#
[Sources.X64]
  X64/
[Packages]
  MdePkg/
  BeniPkg/

The code here is no different from ordinary inf, it is just the specified source fileSourcesA little different, usually when specifiedIa32stillX64

Assembly code writing:

  SECTION .text
;------------------------------------------------------------------------------
; VOID
; EFIAPI
; AsmNop (
;   VOID
;   );
;------------------------------------------------------------------------------
global ASM_PFX(AsmNop)
ASM_PFX(AsmNop):
  nop
  ret

SECTION .textThe code segment is specified, and the assembly code is followed.

global ASM_PFXSpecifies a global function that can be called externally, and of course, you also need to add a header file:

/**
  Code for nothing.
  @param  NA
  @retval  NA
**/
VOID
EFIAPI
AsmNop (
  VOID
  );

You can use this function afterwards, which is no different from the use of ordinary functions.

Here is another example for outputting a character to the Legacy serial port:

global ASM_PFX(AsmSerialIo)
ASM_PFX(AsmSerialIo):
  mov dx, 03f3h
  mov eax, 0
loop:
  in  al, dx
  bt  eax, 5
  jnc loop      ; Wait until ready
  mov dx, 03f8h
  mov ax, cx    ; cx is the input parameter
  out dx, ax    ; Output the character
  ret

The test code can be found in BeniPkg\DynamicCommand\TestDynamicCommand\.

The above is the detailed content of the use of basic assembly code for UEFI development. For more information about UEFI development assembly code, please pay attention to my other related articles!