我发现了一个名为:ProgrammingGroundUp-1-0-booksize.pdf的pdf文件,其中一个项目是制作一个汇编程序,它接收文件并将它们转换为大写,
.section .data
#######CONSTANTS########
#system call numbers
.equ SYS_OPEN, 5
.equ SYS_WRITE, 4
.equ SYS_READ, 3
.equ SYS_CLOSE, 6
.equ SYS_EXIT, 1
#options for open (look at
#/usr/include/asm/fcntl.h for
#various values. You can combine them
#by adding them or ORing them)
#This is discussed at greater length
#in "Counting Like a Computer"
.equ O_RDONLY, 0
.equ O_CREAT_WRONLY_TRUNC, 03101
#standard file descriptors
.equ STDIN, 0
.equ STDOUT, 1
.equ STDERR, 2
#system call interrupt
.equ LINUX_SYSCALL, 0x80
.equ END_OF_FILE, 0
#This …Run Code Online (Sandbox Code Playgroud) 我有一个64位的Ubuntu操作系统,我一直在学习32位汇编.我正在尝试编译这两个文件:
square.s:
#square.s
.section .text
.globl sqr
.type sqr, @function
sqr:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
imull %eax, %eax
popl %ebp
ret
Run Code Online (Sandbox Code Playgroud)
main.c:
//main.c
#include <stdio.h>
extern long sqr(long);
int main(int argc, char* argv[])
{
long squared = sqr(10);
printf("%lu\n", squared);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我的32位虚拟机上,我使用此命令编译它们
gcc main.c square.s -o test
Run Code Online (Sandbox Code Playgroud)
它起作用了.我遇到的问题是我想在我的64位机器上编译这些文件.我已经尝试了几种编译这些文件的方法,但没有一种方法可行.谁能指出我正确的方向?有没有选择这样做?我试过-m32但是没有用.
当我这样做:
gcc -m32 -o test main.c square.s
Run Code Online (Sandbox Code Playgroud)
我明白了:
In file included from /usr/include/stdio.h:28:0,
from main.c:1:
/usr/include/features.h:323:26: fatal error: bits/predefs.h: No such file or directory …Run Code Online (Sandbox Code Playgroud) 我是x86程序集的新手,我正在尝试理解本文档中的代码:http://www.cs.cmu.edu/~410-s07/p4/p4-boot.pdf第3页:
movw $0x1234, %ax
movw %ax, %ds
movw $0x5678, %bx
# The following instruction is the same as "movw $0x1337, (%bx)".
movw $0x1337, %ds:(%bx) # Places 0x1337 into memory word 0x179b8.
# Segment Base: %ds << 4: 12340
# Offset: %bx: + 5678
# -------
# Linear Address: 179b8
Run Code Online (Sandbox Code Playgroud)
但我不理解这个命令:
movw $0x1337, %ds:(%bx) # Places 0x1337 into memory word 0x179b8.
Run Code Online (Sandbox Code Playgroud)
为什么将%ds与(%bx)连接与((%ds << 4)|%bx)相同?
由于我处于实模式(16位),串联不应该是%ds << 8?而不是%ds << 4?
为什么括号只是%bx左右?而不是整个结构,如:movw $ 0x1337,(%ds:%bx)?
我目前正在尝试理解汇编语言中宏的概念.我大学的幻灯片说明如下:
# How to define a macro:
.macro write string
movl string, %esi
call printstr
.endm
# How to use a macro:
write aString
Run Code Online (Sandbox Code Playgroud)
但是,这对我不起作用.我正在使用gcc编译我的代码.
.data
msg: .string "The result is %d.\n"
.text
.global main
.macro add_3 n
movl n, %eax
addl $3, %eax
.endm
main:
add_3 $39
pushl %eax
pushl $msg
call printf
popl %eax
popl %eax
movl $1, %eax
int $0x80
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我收到以下错误:
undefined reference to `n'
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
.syntax unified
.thumb
.cpu cortex-m4
.arch armv7e-m
.fpu fpv4-sp-d16
/* Changes from unprivileged to privileged mode. */
.thumb_func
.section .kernel
.global raise_privilege
.type raise_privilege, %function
raise_privilege:
mrs r0, control
bic r0, r0, #1
msr control, r0
dsb
isb
bx lr
Run Code Online (Sandbox Code Playgroud)
这是手臂组装代码的一部分.我可以查看芯片手册来弄清楚说明的含义.但我不知道如何弄清楚汇编程序指令的行为.thumb_func.更重要的是,我也不知道如何使用这部分代码,它看起来不像常规功能.所以我不知道如何"召唤"它.
我已经将一些汇编功能移植到了64位ARM,并且它们在Android上也能正常工作,但是当我尝试在Xcode中编译相同的文件时,我发现clang使用了不同的语法(与官方ARM不同)文档)。
我发现一些脚本可以将源文件从一种格式转换为另一种格式,但这不是理想的解决方案(当源文件包含预处理器定义时,这些脚本似乎不起作用)。
我可以简单地在Xcode中使用gas还是将clang配置为接受gas语法?如果不是,那么clang汇编器文档在哪里?
更新-2015年9月
看来该问题已由XCode 7(新的clang版本?)解决了:现在,我可以导入为Android编写的程序集源文件,并且它们进行编译时无需进行任何更改。
目前我正在使用汇编语言.我想打印一个字符到控制台,我的程序运行没有错误,但它没有输出.这是我目前的代码:
movl $4, %eax #Defines Output
movl $1, %ebx #STDOUT as first parameter
movl $48, %ecx #Copy char (0) to ECX
movl $1, %edx #String length 1
int $0x80 #Trigger Interrupt
movl %eax, %ebx #Exitcode 0
movl $1, %eax #System Code SYS_EXIT
int $0x80 #Trigger Interrupt
Run Code Online (Sandbox Code Playgroud)
你有什么想法,为什么它没有输出?如您所见,我正在使用GAS语法.我的代码有什么问题?
我正在尝试编写一个简单的汇编程序.出于某种原因,条件移动似乎给了我这个错误.如果我用普通的mov指令替换它们就行了.以下代码有什么问题?
.section .data
supported:
.asciz "AVX is supported"
notsupported:
.asciz "AVX is not supported"
.section .text
.global main
main:
movq $1, %rax
cpuid
andq $0x10000000, %rcx
cmovnz $supported, %rdi
cmovz $notsupported, %rdi
callq puts
movq $0, %rax
ret
cpuid.S:15: Error: operand type mismatch for `cmovnz'
cpuid.S:16: Error: operand type mismatch for `cmovz'
Run Code Online (Sandbox Code Playgroud) 执行以下操作的正确gnu汇编语法是什么:
.section .data2
.asciz "******* Output Data ********"
total_sectors_written: .word 0x0
max_buffer_sectors: .word ((0x9fc00 - $data_buffer) / 512) # <=== need help here
.align 512
data_buffer: .asciz "<The actual data will overwrite this>"
Run Code Online (Sandbox Code Playgroud)
具体来说,我正在编写一个玩具OS。上面的代码是16位实模式。我正在设置一个数据缓冲区,该缓冲区将转储回启动磁盘。我想计算之间的扇区数data_buffer放置在内存中的位置与该数据缓冲区的上限。(地址0x9fc00是缓冲区将运行到保留用于其他目的的RAM的位置。)
我知道我可以编写汇编代码来计算这个;但是,由于它是在构建时就知道的常数,所以我很好奇能否让汇编器为我计算它。
我遇到了三个具体问题:
(1)如果我使用$data_buffer此错误:
os_src/boot.S: Assembler messages:
os_src/boot.S:497: Error: missing ')'
os_src/boot.S:497: Error: can't resolve `L0' {*ABS* section} - `$data_buffer' {*UND* section}
Run Code Online (Sandbox Code Playgroud)
我会感到困惑,因为$当我想要标签的内存地址时,应该使用它,对吗?
(2)如果我使用data_buffer而不是$data_buffer,则会出现此错误:
os_src/boot.S: Assembler messages:
os_src/boot.S:497: Error: missing ')'
os_src/boot.S:497: Error: value of 653855 too …Run Code Online (Sandbox Code Playgroud) 这是一个以前没有发生过的问题.我很确信这可能是我的包装回购的一个问题(我最近重新安装了我的Arch系统,而这刚刚开始发生).
我在x86_64中写了一个小小的hello世界:
.data
str: .asciz "Test"
.text
.globl main
main:
sub $8, %rsp
mov $str, %rdi
call puts
add $8, %rsp
ret
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用GCC进行汇编和链接 - 就像我过去做过很多次一样 - 简单地说:
gcc test.s -o test
然后输出此错误:
/ usr/bin/ld:/tmp/ccAKVV4D.o:在创建共享对象时,不能使用针对`.data'的重定位R_X86_64_32S; 使用-fPIC/usr/bin/ld重新编译:最终链接失败:输出collect2上的不可表示的部分:错误:ld返回1退出状态
这个错误从来没有发生在我身上.我试图通过谷歌搜索相同的错误消息解决问题,但它提出了具体的事情,而我认为这是一个普遍的问题.我已经尝试重新安装base-devel和整个GCC工具链.我不知道我还能做什么(请不要建议使用nasm,这是异端邪说).
我想我错过了一些明显的东西,但是我已经将GCC用于我的装配需求很长一段时间了.