Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:17:41+00:00 2026-06-07T00:17:41+00:00

I keep reading that in order for one to perform integer/floating point division on

  • 0

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'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:17:42+00:00Added an answer on June 7, 2026 at 12:17 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep reading that object-oriented programming can basically be done in any programming language,
Is the current Scala Eclipse plugin ( http://www.scala-lang.org/node/94 ) usable? I keep reading that
I keep reading that I have to use svcUtil.exe, crmSvcUtil.exe and company with tons
I keep reading everywhere that when you ask for dependencies to be injected in
I keep reading that, in C, using pointer arithmetic is generally faster than subscripting
I keep reading that SQL Azure doesn't support relational databases; however, I just tapped
I mostly use Java and generics are relatively new. I keep reading that Java
I am reading fortran 77 code (which I hate!) and in order to keep
In one of my sites, I need to use PHP 'foreach' to keep including
I keep reading how everyone states to return a XmlDocument when you want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.