Assembly x64 Intro - Align 16 of Nasm



1.   addr.S


        extern printf  ; the C function to be called

%macro pabc 1   ; a "simple" print macro
 section .data
.str db %1,0  ; %1 is first actual in macro call
 section .text
        mov     rdi, fmt4 ; first arg, format
 mov rsi, .str ; second arg
 mov     rdx, a        ; a' address rather than its value
 mov     rcx, b        ; b' address rather than its value

 mov     r8, [c]         ; fifth arg
 mov     rax, 0         ; no xmm used
 call    printf  ; Call C function
%endmacro
 
 section .data    ; preset constants, writable
align   16
a: dq 3  ; 64-bit variable a initialized to 3
b: dq 4  ; 64-bit variable b initializes to 4
fmt4: db "%s, a=%p, b=%p, c=%ld",10,0 ; format string for printf
 
 section .bss   ; uninitialized space
c: resq 1  ; reserve a 64-bit word

 section .text  ; instructions, code segment
 global  main  ; for gcc standard linking
main:    ; label
 push  rbp  ; set up stack
lit5:    ; c=5;
 mov rax,5   ; 5 is a literal constant
 mov [c],rax  ; store into c
 pabc "c=5  "  ; invoke the print macro
 
addb:    ; c=a+b;
 mov rax,[a]   ; load a
 add rax,[b]  ; add b
 mov [c],rax  ; store into c
 pabc "c=a+b"  ; invoke the print macro
 
subb:    ; c=a-b;
 mov rax,[a]   ; load a
 sub rax,[b]  ; subtract b
 mov [c],rax  ; store into c
 pabc "c=a-b"  ; invoke the print macro
 
mulb:    ; c=a*b;
 mov rax,[a]   ; load a (must be rax for multiply)
 imul qword [b] ; signed integer multiply by b
 mov [c],rax  ; store bottom half of product into c
 pabc "c=a*b"  ; invoke the print macro
 
diva:    ; c=c/a;
 mov rax,[c]   ; load c
 mov rdx,0  ; load upper half of dividend with zero
 idiv qword [a] ; divide double register edx rax by a
 mov [c],rax  ; store quotient into c
 pabc "c=c/a"  ; invoke the print macro

 pop rbp  ; pop stack
        mov     rax,0           ; exit code, 0=normal
 ret   ; main returns to operating system


2.   compile

nasm -f elf64 addr.S

gcc -o addr.x addr.o


3. run

./addr.x


c=5  , a=0x601040, b=0x601048, c=5
c=a+b, a=0x601040, b=0x601048, c=7
c=a-b, a=0x601040, b=0x601048, c=-1
c=a*b, a=0x601040, b=0x601048, c=12
c=c/a, a=0x601040, b=0x601048, c=4


可以看到,  a 的地址是 16 位对齐的, 而 b 的 不是。


4. 修改代码, 设置 b align 16 属性

        section .data           ; preset constants, writable
align   16
a:      dq      3               ; 64-bit variable a initialized to 3
align   16
b:      dq      4               ; 64-bit variable b initializes to 4
fmt4:   db "%s, a=%p, b=%p, c=%ld",10,0 ; format string for printf


5. 重新编译运行


c=5  , a=0x601040, b=0x601050, c=5
c=a+b, a=0x601040, b=0x601050, c=7
c=a-b, a=0x601040, b=0x601050, c=-1
c=a*b, a=0x601040, b=0x601050, c=12
c=c/a, a=0x601040, b=0x601050, c=4


现在可以看到, a, b 的地址都是 16位对齐了。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* 全局样式 */ body { font-family: Arial, sans-serif; font-size: 16px; color: #333; margin: 0; } a { color: #333; text-decoration: none; } a:hover { color: #555; } ul, ol { margin-top: 0; margin-bottom: 10px; } ul li, ol li { margin-left: 20px; } /* 头部样式 */ header { color: #fff; padding:0 0 0 0; } .container { max-width: 1660px; margin: 0 auto; padding: 0 20px; } #hero { background-image: url(QMZYWY/images/wy.jpg); background-size: cover; background-position: center; color: #fff; text-align: center; padding: 100px 0; } h1 { margin: 0; font-size: 32px; } nav { display: flex; justify-content: flex-end; } nav ul { list-style: none; margin: 0; padding: 0; display: flex; } nav li { margin-right: 20px; } nav a { color: #fff; text-decoration: none; padding: 5px; border-radius: 5px; transition: background-color 0.2s ease; } nav a:hover { background-color: #555; } /* 英雄介绍样式 */ .hero-intro { background-color: #fff; padding: 40px 0; } .hero-intro-text { margin-bottom: 20px; } .hero-intro-image { text-align: center; } .hero-intro-image img { max-width: 100%; height: auto; } /* 游戏攻略样式 */ .game-strategy { background-color: #f5f5f5; padding: 40px 0; } .game-strategy p { margin-bottom: 20px; } /* 页脚样式 */ footer { background-color: #222; color: #fff; padding: 10px 0; } footer p { margin: 0; text-align: center; } /* 响应式样式 */ @media screen and (max-width: 768px) { .container { padding: 0 10px; } h1 { font-size: 24px; } nav { justify-content: center; } nav li { margin-right: 10px; } .hero-intro { padding: 20px 0; } .hero-intro-text { text-align: center; } .hero-intro-image { margin-top: 20px; } } @media screen and (min-width: 768px) { .hero { background-image: url('QMZYWY/images/wy.jpg'); } }在此代码中加入网页背景图片响应式
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值