add GAS templates

This commit is contained in:
Roy Qu 2023-02-12 13:24:51 +08:00
parent 0e3441e604
commit 2660096377
14 changed files with 125 additions and 13 deletions

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -1,11 +1,11 @@
[Template]
Ver = 3
Name = C and ASM
Name = GAS & C
Category = Assembly
Description = C and ASM mixing programming demo
Name[zh_CN] = C汇编混合
Description = C and GAS mixing programming demo
Name[zh_CN] = GAS与C
Category[zh_CN] = 汇编
Description[zh_CN] = C和汇编混合编程示例
Description[zh_CN] = C和GAS汇编混合编程示例
Icon = app.ico
@ -18,8 +18,8 @@ UnitCount = 2
[Unit0]
Source = utils.asm
Target = utils.asm
Source = utils.s
Target = utils.s
[Unit1]

View File

@ -0,0 +1,15 @@
.global maxofthree
.global add3
.text
maxofthree:
mov %ecx, %eax # result (rax) initially holds x
cmp %edx, %eax # is x less than y?
cmovl %edx, %eax # if so, set result to y
cmp %r8d, %eax # is max(x,y) less than z?
cmovl %r8d, %eax # if so, set result to z
ret # the max will be in rax
add3:
mov %ecx, %eax
add %edx, %eax
add %r8d, %eax
ret

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -1,11 +1,11 @@
[Template]
Ver = 3
Name = Hello ASM
Name = Hello GAS
Category = Assembly
Description = A simple asm program
Name[zh_CN] = ASM你好
Description = A simple GNU as program
Name[zh_CN] = GAS你好
Category[zh_CN] = 汇编
Description[zh_CN] = 简单的汇编程序示例
Description[zh_CN] = 简单的GNU汇编程序示例
Icon = app.ico
@ -18,5 +18,5 @@ UnitCount = 1
[Unit0]
Source = main.asm
Target = main.asm
Source = main.s
Target = main.s

View File

@ -0,0 +1,22 @@
.global main
.text:
main:
# the x64 calling convention requires you to allocate 32 bytes of shadow space before each call
# https://stackoverflow.com/questions/30190132/what-is-the-shadow-space-in-x64-assembly/
sub $32, %rsp # allocate shadow space
leaq fmt(%rip), %rax # first parameter
movq %rax, %rcx
leaq msg(%rip), %rax # second parameter
movq %rax, %rdx
call printf
add $32, %rsp # remove shadow space
xor %eax,%eax # set 0 as exit code
ret
msg:
.asciz "Hello world\n" # asciz puts a 0 byte at the end
fmt:
.asciz "%s" # asciz puts a 0 byte at the end

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -0,0 +1,22 @@
[Template]
Ver = 3
Name = Hello NASM
Category = NASM
Description = A simple nasm program
Name[zh_CN] = NASM你好
Category[zh_CN] = NASM
Description[zh_CN] = 简单的NASM汇编程序示例
Icon = app.ico
[Project]
Type = 1
IsCpp = true
Encoding = UTF-8
ClassBrowserType = 0
UnitCount = 1
[Unit0]
Source = main.asm
Target = main.asm

View File

@ -10,7 +10,7 @@ section .text:
main:
; the x64 calling convention requires you to allocate 32 bytes of shadow space before each call
; https://stackoverflow.com/questions/30190132/what-is-the-shadow-space-in-x64-assembly/
sub rsp, 32 ; allocate shadow space for call
sub rsp, 32 ; allocate shadow space
mov rcx, fmt ; first parameter
mov rdx, msg ; secodng parameter
call printf

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -0,0 +1,27 @@
[Template]
Ver = 3
Name = NASM & C
Category = NASM
Description = C and NASM mixing programming demo
Name[zh_CN] = NASM与C
Category[zh_CN] = NASM
Description[zh_CN] = C和NASM汇编混合编程示例
Icon = app.ico
[Project]
Type = 1
IsCpp = true
Encoding = UTF-8
ClassBrowserType = 0
UnitCount = 2
[Unit0]
Source = utils.asm
Target = utils.asm
[Unit1]
Cpp = main.cpp
CppName = main.cpp

View File

@ -0,0 +1,26 @@
#include <stdio.h>
#ifdef __cplusplus
#define ASM_FUNC extern "C"
#else
#define ASM_FUNC
#endif
ASM_FUNC int maxofthree(int, int, int);
ASM_FUNC int add3(int, int, int);
int main() {
printf("%d\n", add3(1, -4, -7));
printf("%d\n", add3(1, 2, 3));
printf("%d\n", add3(2, -6, 1));
printf("------\n");
printf("%d\n", maxofthree(1, -4, -7));
printf("%d\n", maxofthree(2, -6, 1));
printf("%d\n", maxofthree(2, 3, 1));
printf("%d\n", maxofthree(-2, 4, 3));
printf("%d\n", maxofthree(2, -6, 5));
printf("%d\n", maxofthree(2, 4, 6));
return 0;
}