| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
买学问 - 大佬一对一辅导,有问必答买学问 - 专业的付费知识问答平台
共有 1068 人关注过本帖
标题:小白求助如何打印变量值
只看楼主 加入收藏
batsom
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2021-9-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:7 
小白求助如何打印变量值
section .data
massage:   dw "1+2+...+100="
Len: equ $ - massage
valueToPrint: db 0

;section .bss
 ;valueToPrint: resb 4

section .text
global main

main:
   mov ebp, esp; for correct debugging
   mov eax,0
   mov ebx,0
   mov ecx,100
s:
   add ebx,1
   add eax,ebx
   loop s

;push eax

;sub eax,5H
;add eax,48H
mov [valueToPrint],eax

mov eax,4 ; 4号调用 sys_write的系统调用
mov ebx,1 ; ebx送1表示输出,参数1,文件描述符,stdout是1
mov ecx,massage ; 字符串的首地址送入ecx
mov edx,Len ; 字符串的长度送入edx
int 80h ; 输出字串

mov eax,4
mov ebx,1
mov ecx,valueToPrint
mov edx,4
int 0x80


mov ebx,0
mov eax,1
int 0x80
搜索更多相关主题的帖子: add mov 打印 变量值 int 
2021-09-20 15:07
batsom
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2021-9-20
收藏
得分:0 
唉!换了个方式:

section .data
massage:   dw "1+2+...+100=%d"

;section .bss
; valueToPrint: resb 4

section .text

global main
extern printf
extern exit

main:
   mov ebp, esp; for correct debugging
   mov eax,0
   mov ebx,0
   mov ecx,1000
s:
   add ebx,1
   add eax,ebx
   loop s

push eax
push massage
call printf

push  0
call exit     ; exit(0)
2021-09-20 19:00
自由而无用
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:10
帖 子:258
专家分:739
注 册:2021-8-9
收藏
得分:2 
sys_write is an interrupt call, different from printf, if you wanna use it to implement the same function of printf(%d)
there is a way:
insert result(5050) into the string message, just like:

msg     db '1 + 2 + ... + 100 = %d'
mov esi, offset msg
mov byte ptr ds:[esi + 21], 35h
mov byte ptr ds:[esi + 22], 30h
mov byte ptr ds:[esi + 23], 35h
mov byte ptr ds:[esi + 24], 30h

I have tried serval times, but unsuccessful, cause of the weird syntax of nasm, maybe I will try later someday, but this way is definitely correct
2021-09-20 20:02
自由而无用
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:10
帖 子:258
专家分:739
注 册:2021-8-9
收藏
得分:2 
if you have time to take a look of nasm syntax tutorial about how to write a imm into a memory, that will be a much better way
for example:
mov byte ptr ds:[esi], imm ???

May the force be with you! good luck!

[此贴子已经被作者于2021-9-20 20:31编辑过]

2021-09-20 20:06
自由而无用
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:10
帖 子:258
专家分:739
注 册:2021-8-9
收藏
得分:4 
sum code like this looks much more clearly
   xor eax,eax
   xor ebx,ebx
   mov ecx,64H
s:
   inc ebx
   add eax,ebx
   loop s
2021-09-20 20:16
自由而无用
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:10
帖 子:258
专家分:739
注 册:2021-8-9
收藏
得分:4 
ok, I know how to make it works now
//online parser: https://www.bccn.net/run/ (select assembly)
程序代码:
section .data                          ;section declaration
msg     db  "1 + 2 + ... + 100 = %d  " ;our dear string
len     equ $ - msg                    ;length of our dear string
section .text                          ;section declaration
                       ;we must export the entry point to the ELF linker or
   global _start       ;loader. They conventionally recognize _start as their
                       ;entry point. Use ld -e foo to override the default.
_start:
;write our string to stdout
       xor eax,eax
       xor ebx,ebx
       mov ecx,64H
_sum:
       inc ebx
       add eax,ebx
       loop _sum
       
       lea     esi,[msg]
       ;5050 = 13BAH
       add     eax, +2176H         ;hard code 4 print tst
       mov     byte [esi + 20],ah  ;masm syntax: mov byte ptr ds:[esi + 20], ah
       mov     byte [esi + 21],al
       mov     byte [esi + 22],ah
       mov     byte [esi + 23],al
       mov     eax,4   ;system call number (sys_write)
       mov     ebx,1   ;first argument: file handle (stdout)
       mov     ecx,msg ;second argument: pointer to message to write
       mov     edx,len ;third argument: message length
       int     0x80    ;call kernel
;and exit
       mov     eax,1   ;system call number (sys_exit)
       xor     ebx,ebx ;first syscall argument: exit code
       int     0x80    ;call kernel


output sample:
1 + 2 + ... + 100 = 5050

[此贴子已经被作者于2021-9-20 22:03编辑过]

2021-09-20 21:52
自由而无用
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:10
帖 子:258
专家分:739
注 册:2021-8-9
收藏
得分:4 
and one last thing is that, you cant consider the sys_write as printf, they have different in-stack arguments, different forms, totally different, one(int80) in core state, the other(printf) in user state,however, you made a great try
2021-09-20 22:02
Valenciax
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:11
帖 子:305
专家分:2212
注 册:2016-5-15
收藏
得分:4 
可以换个思路,就是直接吧变量换成字符串,连结原来的字符串一拼输出

massage:   db "1+2+...+100="
valueToPrint: db 0,0,0,0,0
Len: equ $ - massage

...
...
 ;以下用除10取余法,由值转换字串

程序代码:

 mov edi,valueToPrint    ;变量字符地址
 mov ecx,0     ;清0
 mov ebx,10     ;除法准备
x1:

 mov edx,0     ;清0
 div ebx     ;eax /10 ,若1234 ,除10后,dl得余数4,
 push edx     ;保存, eax=1234,依次保存4,3,2,1
 inc ecx     ;累加个数
 or eax,eax     ;是否已除尽
 jnz x1     ;不是,再除
x2:

 pop eax      ;后入先出,先印出第一数,然后第二....
 or al,30h     ;转ascii
 stosb         ;存入字串缓冲es:edi
 loop ax2     ;下一个

; 到此valueToPrint就已是变量字符串,再系统调用输出massage字串
...
...
2021-09-22 05:58
快速回复:小白求助如何打印变量值
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058792 second(s), 8 queries.
Copyright©2004-2021, BCCN.NET, All Rights Reserved