乘两个值,并将其打印到屏幕(NASM和Linux) [英] Multiplying two values and printing them to the screen (NASM, Linux)
问题描述
我继续读,为了让一个执行整数/浮点除法对寄存器,而在需要执行的寄存器(县)实际上是初始化
。我很好奇,适当的汇编指令是做这算什么。我简单地通过类似提供一个地址:
MOV ECX,0x65F。0x65F重新presents地址为ECX指向
然后及时(后来在code)做这样的事情:
字节MOV [ECX],为0xA;移动是0xA值到ECX的内容,只使用一个字节的数据价值
这是要执行这样的操作的正确方法?如果没有,是什么?
更新
好了,所以我想要做的是乘法基本上两个值,并将其打印到屏幕上。
在code是如下,由于某种原因,每次我试图分裂 EDX
我得到无论是段错误或浮点算术异常。可能有人给我解释一下它是什么,我做错了?
code
部分。数据
计数器:DB是0xA;在反存储值10,只分配一个字节。这将用于递减的目的
.bss段
valueToPrint:RESB 4;页头4个字节'valueToPrint数据.text段全球_start_print_char:
添加EAX,'0';转换为ASCII
MOV [valueToPrint],EAX;在valueToPrintEAX的商店内容
MOV EAX,4; SYSWRITE
MOV EBX,1;标准输出
MOV ECX,valueToPrint;机器会采取任何的价值存在于ECX和打印
MOV EDX,1,打印只是单字节的价值的数据
0x80的诠释;调用内核亲热指令
RET_convert_values:
MOV EDX,为0xA;除以10,EAX,这将降低其十位
DIV EDX;(这里**程序崩溃**)做除法:余应存放在EDX
MOV字节[EDX],为0x0零出EDX
呼吁_print_char;对于最新的字符做印刷
十二月字节[计数器];递减计数器
MOV DWORD [EAX],计数器;在EAX商场专柜
JNZ _convert_values;而EAX> 0继续处理_endl:
MOV EAX,'\\ n';在EAX店换行符被打印
调用_print_char;打印值
RET_mul:
MOV EAX,0x2A;存储42 EAX
MOV EDX,0x2B访问;存储43 EDX
MUL EDX;乘法[EAX] * [EDX]
RET_safe_exit:
MOV EAX,1;启动退出系统调用
MOV EBX,0;错误code 0退出
0x80的诠释;调用内核来完成其投标_开始:
NOP;用于保持GDB从抱怨 调用_mul;乘值
调用_convert_values;做十六进制ASCII转换 JMP _safe_exit;使用JMP,而不是打电话,因为它在技术上不'RET'
我们在聊天中谈到分开......
下面是一个工作版本一起玩。
它有一个微妙的问题。你可以找到它?
你能解释为什么它做什么呢?
;两个数相乘,显示ASCII /十进制
;
; (因为我有一个64位的系统,这迫使32位code)
32位
;
.text段
;
; _start是唯一的标签,您必须$ P $ _ PPEND
;其他人可能是库函数(例如:_printf,_exit)
;纯组装只需要_start,如果用glibc链接
;通常需要INSTEAD _main的_start
;
全球_start
;
;
_开始:
NOP;占位符GDB的调试中断
;
调用MUL;乘值
调用convert_values;做十六进制ASCII转换
;
JMP safe_exit;使用JMP,而不是打电话,因为它在技术上不'RET'
;
;
;子程序/函数跟随
;
MUL:
MOV EAX,0x2A;存储42 EAX
MOV EDX,0x2B访问;存储43 EDX(42 * 43 = 1806)
MUL EDX; EAX相乘* EDX,导致EDX:EAX
RET
;
;这个程序不会处理从'MUL'伸入EDX BIG值
;我们正在学习,不要使事情繁衍到超过42十亿杂交
convert_values:
MOV EDX,0;值实际上EDX:EAX,EDX零
MOV ECX,0x0A的;鸿沟EDX:EAX 10
IDIV ECX;导致EAX,余数在EDX
推EAX;节省堆栈值
MOV EAX,EDX;把其余的(0-9)在EAX
添加EAX,'0';转换值的ASCII字符
调用PRINT_CHAR;打印最新的性格
弹出EAX;恢复值
或EAX,EAX;根据EAX值设置标志
JNZ convert_values;而EAX = 0继续处理!
;
; NASM不转换\\ n为进LF ......仅仅使用10,相当于
ENDL:
MOV EAX,10;在EAX店换行符被打印
调用PRINT_CHAR;打印值
RET
;
PRINT_CHAR:
MOV [valueToPrint],EAX;在EAX的商店内容[valueToPrint]
MOV EAX,4; SYSWRITE
MOV EBX,1;标准输出
MOV ECX,valueToPrint;机器会采取任何价值[ECX]和打印存在
MOV EDX,1,打印只是单字节的价值的数据
0x80的诠释;调用内核亲热指令
RET
;
safe_exit:
MOV EAX,1;启动退出系统调用
MOV EBX,0;错误code 0退出
0x80的诠释;调用内核来完成其投标
;
; =====================================
.bss段
;本节不分配,只保留。
;自动置零程序启动时
;
; ALLOC 4个字节'valueToPrint数据
valueToPrint:
RESD 1; 1 RESD = 4 RESB(DWORD /字节)
;
;
尾翼警报...
据打印出结果BACKWARDS!结果
为了解决这个问题,我们将不得不重新设计如何获得数字
并存储在打印之前。
块引用>
块引用>我直接发送电子邮件这个给你,有一些额外的音符一起。
I keep reading that in order for one to perform integer/floating point division on a register, the register(s) being performed on need to actually be
initialized
. I'm curious to what the proper assembler directive is to do this. Do I simply provide an address by something like:
mov ecx, 0x65F ;0x65F represents an address for ecx to point to
.And then promptly (later in code) do something like:
mov byte [ecx], 0xA ;move the value of 0xA into the contents of ecx, using only a byte's worth of data
Is this the proper way to perform such an operation? If not, what is?
Update
Ok, so what I'm trying to do is basically multiply two values and print them to the screen. The code is as follows, and for some reason every time I try to divide
edx
I get either a segmentation fault or a floating point arithmatic exception. Could someone explain to me what it is that I'm doing wrong?Code
section .data counter: db 0xA ;store value 10 in 'counter', while allocating only one byte. This will be used for decrementing purposes section .bss valueToPrint: resb 4 ;alloc 4 bytes of data in 'valueToPrint' section .text global _start _print_char: add eax, '0' ;convert to ascii mov [valueToPrint], eax ;store contents of 'eax' in valueToPrint mov eax, 4 ;syswrite mov ebx, 1 ;stdout mov ecx, valueToPrint ;machine will take whatever value exists in 'ecx' and print mov edx, 1 ;print only a single byte's worth of data int 0x80 ;invoke kernel to perfrom instruction ret _convert_values: mov edx, 0xA ;dividing eax by 10, which will lower its tens place div edx ;(**Program crash here**)do division: remainder SHOULD be stored in edx mov byte [edx], 0x0 ;zero out edx call _print_char ;do printing for latest character dec byte [counter] ;decrement counter mov dword [eax], counter ;store counter in eax jnz _convert_values ;while eax > 0 continue process _endl: mov eax, '\n' ;store newline character in eax to be printed call _print_char ;print value ret _mul: mov eax, 0x2A ;store 42 in eax mov edx, 0x2B ;store 43 in edx mul edx ;multiply [eax] * [edx] ret _safe_exit: mov eax, 1 ;initiate 'exit' syscall mov ebx, 0 ;exit with error code 0 int 0x80 ;invoke kernel to do its bidding _start: nop ;used to keep gdb from complaining call _mul ;multiply the values call _convert_values ;do hex to ascii conversion jmp _safe_exit ;use jmp as opposed to call since it technically doesn't 'ret'
解决方案We spoke separately in chat....
Here's a working version to play with.
It has a subtle problem. Can you find it? Can you explain WHY it does what it does?
; Multiply two numbers, display in ascii/decimal ; ; (because I have a 64bit system, this forces 32bit code) bits 32 ; section .text ; ; _start is the ONLY label you MUST prepend _ ; others might be library functions (ex: _printf,_exit) ; pure assembly only needs _start, if linked with glibc ; typically need _main INSTEAD of _start ; global _start ; ; _start: nop ;placeholder for gdb's debug interrupt ; call mul ;multiply the values call convert_values ;do hex to ascii conversion ; jmp safe_exit ;use jmp as opposed to call since it technically doesn't 'ret' ; ; ; subroutines / functions follow ; mul: mov eax, 0x2A ;store 42 in eax mov edx, 0x2B ;store 43 in edx (42*43=1806) mul edx ;multiply eax*edx, result in edx:eax ret ; ; this routine doesn't handle BIG values from 'mul' which extend into edx ; we're learning, don't make things multiply out to more than 4.2 billion-ish convert_values: mov edx,0 ;value actually edx:eax, zero edx mov ecx,0x0A ;divide edx:eax by 10 idiv ecx ;result in eax, remainder in edx push eax ;save value on stack mov eax,edx ;put remainder (0-9) in eax add eax,'0' ;convert value to ascii character call print_char ;print the latest character pop eax ;restore value or eax,eax ;set flags based on eax value jnz convert_values ;while eax != 0 continue process ; ; nasm doesn't convert \n into LF... just use 10, equivalent endl: mov eax, 10 ;store newline character in eax to be printed call print_char ;print value ret ; print_char: mov [valueToPrint], eax ;store contents of 'eax' in [valueToPrint] mov eax, 4 ;syswrite mov ebx, 1 ;stdout mov ecx, valueToPrint ;machine will take whatever value exists in [ecx] and print mov edx, 1 ;print only a single byte's worth of data int 0x80 ;invoke kernel to perfrom instruction ret ; safe_exit: mov eax,1 ;initiate 'exit' syscall mov ebx,0 ;exit with error code 0 int 0x80 ;invoke kernel to do its bidding ; ; ===================================== section .bss ; this section is not allocated, just reserved. ; Automatically set to zero when program starts ; ; alloc 4 bytes of data in 'valueToPrint' valueToPrint: resd 1 ; 1 resd=4 resb (Dword/Byte) ; ;
Spoiler Alert...
It prints the result BACKWARDS!
To fix this, we'll have to redesign how the digits are obtained and stored before printing.
I'm emailing this directly to you, along with some additional notes.
这篇关于乘两个值,并将其打印到屏幕(NASM和Linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!