Printing out decimal value in 8086 Assembly Language

时间:2016-02-12 22:13:14

标签: assembly x86 masm x86-16 emu8086

I am currently working on a project that requires me to prompt a user for three inputs (length, width, & height) and then calculate the volume (lwh). I am having problems printing out the result after the calculations are complete. Is there a way to print out a decimal value?

  Template.chat_page.helpers({
    messages:function(){
      var chat = Chats.findOne({_id:Session.get("chatId")});
      return chat.messages;
    }, 
    other_user:function(){
      return ""
    }, 

  })


  Template.chat_message.helpers({
    user:function(){
    var currentId = Session.get('chatId');
    console.log(Chats.findOne({_id:Session.get("chatId")}));
    console.log(Meteor.user()._id);
    return Meteor.user()._id;
      },
  });

2 个答案:

答案 0 :(得分:2)

这可以通过除法/模数简单地完成。例如,如果我们将1字节值转换为十进制 - 比如152 - 我们可以先将152除以100,然后对其应用模10,得到1的结果(数字中的第一个数字)

(152 / 100) % 10 = 1

然后我们可以将它保存到字符串缓冲区以便稍后打印,同时我们处理下一个数字。对于下一个数字,我们重复该过程,除了除以10而不是100

(152 / 10) % 10 = 5

将此结果存储在缓冲区的下一个插槽中。重复此过程,直到您将值除以1,此时您可以使用modulo:

152 % 10 = 2

在伪代码中,算法看起来像这样:

buffer: byte[4]
buffer[3] = 0       ;; Null-terminate the buffer
buffer_index = 0

value: 153
divisor: 100        ;; Increase this for values larger than 999

while divisor > 0 do
    buffer[buffer_index] = (value / divisor) % 10
    buffer_index = buffer_index + 1
    divisor = divisor / 10
repeat

print buffer

我会把汇编翻译留给你;)

答案 1 :(得分:1)

EMU8086 包含一组宏,并且有一个功能可以执行您想要的操作。将其添加到程序集文件的顶部:

include "emu8086.inc"

END Main上方添加 BOTH 这些新行:

Main ENDP

DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS

END Main

现在,您需要在代码中的任何位置将有符号整数打印到控制台,您只需将值放在 AX 中即可:

call print_num

要打印无符号整数,您可以执行以下操作:

call print_num_uns

例如,如果您将此代码放在程序中:

mov ax, -10
call print_num

它应该显示在控制台上:

-10

请注意:这些宏和函数是 EMU8086 的一项功能,在其他8086汇编程序中不可用。