x86 Assembly String Buffer Number to ASCII - linux

I was writing an x86 assembly program to output a number in hexadecimal. The program was assembled using nasm and the image file ran by qemu. The behavior of the program confused me a lot. As the working program below suggests, I wouldn't have to add 0x30 to a digit to get it to print the character of that digit.
; Boot sector code offset: 0x7c00
[org 0x7c00]
mov dx, 0x1fb6 ; The hexadecimal to be printed
call print_hex ; call the function
jmp $ ; jump infinitely
%include "print_string.asm" ; Include the print_string function
print_hex:
pusha ; push all registers to stack
mov ax, 0x4 ; rotate through the number four times
print_hex_loop:
cmp ax, 0x0 ; compare the counter with 0
jle print_hex_end ; if it is zero then jump to the end
mov cx, dx ; move dx to cx
and cx, 0x000F ; take the lower four binary digits of cx
cmp cx, 0xa ;compare the digits with 0xa
jge print_hex_letter ; if it is larger than a, jump to printing character
add cx, 0x0 ; otherwise print the ascii of a number
jmp print_hex_modify_string ; jump to routine for modifing the template
print_hex_letter:
add cx, 0x7 ; print the ascii of a letter
print_hex_modify_string:
mov bx, HEX_OUT ; bring the address of HEX_OUT into dx
add bx, 0x1 ; skip the 0x
add bx, ax ; add the bias
add byte [bx], cl ; move the character into its position
shr dx, 4 ; shift right 4 bits
sub ax, 0x1 ; subtract 1 from the counter
jmp print_hex_loop ; jump back to the start of the function
print_hex_end:
mov bx, HEX_OUT ; move the address of HEX_OUT to bx
call print_string ; call the function print_string
popa ; pop all registers from stack
ret ; return to calling function
HEX_OUT:
db '0x0000',0 ; The template string for printing
times 510-($-$$) db 0 ; fill zeros
dw 0xaa55 ; MAGIC_FLAG for boot
boot_sect.asm
print_string:
pusha
mov ah, 0x0e
mov al, [bx]
print_string_loop:
cmp al, 0x0
je print_string_end
int 0x10
add bx, 0x1
mov al, [bx]
jmp print_string_loop
print_string_end:
popa
ret
print_string.asm
The output of this program is what I expected, but when I tried to add 0x30 on the numerals to get the ASCII code of the digits, the output was gibberish. Is there some trick to it or am I missing some key points here?
Thanks!

The answer to your original question:
Because you do add byte [bx], cl to write digit into buffer, and the buffer already contains '0', so the first time it will work correctly. Calling print_hex second time will produce gibberish again, as the HEX_OUT content is already modified (trivia: which hex number printed as first would allow also some second value to be printed correctly?).
Now just for fun I'm adding how I would probably do print_hex for myself. Maybe it will give you additional ideas for your x86 ASM programming, I tried to comment it a lot to explain why I'm doing things in a way I'm doing them:
First I would separate formatting function, so I could eventually reuse it elsewhere, so input is both number and target buffer pointer. I'm using LUT (look up table) for ASCII conversion, as the code is simpler. If you care about size, it's possible to do it in code with branching in less bytes and use the slower pusha/popa to save registers.
format_hex:
; dx = number, di = 4B output buffer for "%04X" format of number.
push bx ; used as temporary to calculate digits ASCII
push si ; used as pointer to buffer for writing chars
push dx
lea si,[di+4] ; buffer.end() pointer
format_hex_loop:
mov bx,dx ; bx = temporary to extract single digit
dec si ; si = where to write next digit
and bx,0x000F ; separate last digit (needs whole bx for LUT indexing)
shr dx,4 ; shift original number one hex-digit (4 bits) to right
mov bl,[format_hex_ascii_lut+bx] ; convert digit 0-15 value to ASCII
mov [si],bl ; write it into buffer
cmp di,si ; compare buffer.begin() with pointer-to-write
jb format_hex_loop ; loop till first digit was written
pop dx ; restore original values of all modified regs
pop si
pop bx
ret
format_hex_ascii_lut: ; LUT for 0-15 to ASCII conversion
db '0123456789ABCDEF'
Then for convenience a print_hex function may be added too, providing its own buffer for formatting with "0x" and nul terminator:
print_hex:
; dx = number to print
push di
push bx
; format the number
mov di,HEX_OUT+2
call format_hex
; print the result to screen
lea bx,[di-2] ; bx = HEX_OUT
; HEX_OUT was already set with "0x" and nul-terminator, otherwise I would do:
; mov word [bx],'0x'
; mov byte [bx+6],0
call print_string
pop bx
pop di
ret
HEX_OUT:
db '0x1234',0 ; The template string for printing
And finally example usage from the boot code:
mov dx,0x1fb6 ; The hexadecimal to be printed
call print_hex
mov dx,ax ; works also when called second time
call print_hex ; (but would be nicer to print some space between them)
jmp $ ; loop infinitely
(I did verify this code to some extend (that it will compile and run), although only by separate parts of it and in 32b environment (patching few lines to make it 32b), so some bug may have slipped in. I don't have 16b environment to verify it as complete boot code.)

Related

Decode base64 in nasm

[Link for output after decoding. ][1]The code starts off well and then goes bezerk and then comes back to normal. It is meant to convert base64 nasm into its original form. I turn the first byte received, in ASCII in the rax register, into Base64 to get the binary equivalent. Make room for two bits and then take the next byte in rbx and do the same thing. Then get the top two bits in rbx and add to the first byte to get the ASCII equivalent and save the result. I cannot treat the end of the file with = because of this issue.
Thanks for the help.
; Build using these commands:
; nasm -f elf64 -g -F dwarf decode.asm
; ld -o decode decode.o
%macro writeIt 0
add al,bl ; add bl to al because upper rbx contains the rest
mov byte[B64LIN+rcx-1],al ; move the equivalent into B64LIN
%endmacro
; ************************************************************************
;;; Note: This process does automatic conversion into ASCII ##############
; ************************************************************************
%macro clear 1
xor %1,%1
%endmacro
SECTION .bss ; Section containing uninitialized data
BUFFLEN equ 4
Buff: resb BUFFLEN
SECTION .data ; Section containing initialised data
B64LIN: db "000",0 ; used for output
B64LEN: EQU $-B64LIN ; only used to determine the size of the string treated
SECTION .text ; Section containing code
;;; We convert ASCII to Base 64
Base64:
.UpperCase: ; remove 65 to convert match the B64Table
sub rax,65
ret
.LowerCase: ; remove 71 to convert match the B64Table
sub rax,71
ret
.Numbers: ; add 4 for numbers
add rax,4
ret
.Addition: ; remove 62 to convert match the B64Table
sub rax,62
ret
.BackSlash: ; remove 63 to convert match the B64Table
sub rax,63
ret
;------------------------------------------------------------------------
; Encode: Enconde binary datas into Base 64
; UPDATED: 15/12/2017
; IN: File
; RETURNS: ASCII VERSION
; MODIFIES: ASCII to Base 64
;;; Behaves like a switch statement
;;; Look for the equivalent Base64
Convert:
cmp rax,61h
jge Base64.LowerCase
cmp rax,41h
jge Base64.UpperCase
cmp rax,3Dh
je .EOF1
cmp rax,30h
jge Base64.Numbers
cmp rax, 2Bh
je Base64.Addition
cmp rax,2Fh
je Base64.BackSlash
cmp rax,rbx ; comparing rbx to rax for zeros
je Exit ; we're done if that happens
; *****************************************************************************
;;; Note: This process doesn't send any values back because it was added to the
;;; file in the encoding process. ##############
; *****************************************************************************
.EOF1:
clear rax
writeIt
ret
;;; Register clean up
Decode:
;;; Treating 1st Byte
mov al,byte[Buff+rcx] ; takes in the first element in the input
call Convert ; convert to get their Base64 value
;;; Treating 2nd Byte
inc rcx ; increment to get the next element in the Buff
mov bl,byte[Buff+rcx] ; moves second element into rbx
xchg al,bl ; xchg rax with rbx because Convert deals with rax
call Convert ; call Convert to get the base64 equiv.
xchg bl,al ; get the values back and exchange them
rol rax,2 ; make room for the first 2 bits in rbx
ror rbx,4 ; keep only the top four since first 2 bits are 00
writeIt
clear bl ; clear bl so we can role back
rol rbx,8 ; role 8 so we already make room when we moves rax
;;; Treating 3rd Byte
inc rcx ; increment to get the next element
mov al,byte[Buff+rcx] ; moves it directly into al since previous element is gone
call Convert ; Converts to Base64
ror rax,2 ; roll right to get the top 4 bits only
xchg rax,rbx ; xchg so even the top bits are kept in the process
writeIt
;;; Treating 4th Byte
clear bl ; clear lower 4 bits
clear rax ; clears everything since we have no use for it
inc rcx ; increments to get next
mov al,byte[Buff+rcx] ; moves next Byte into al
call Convert ; converts to Base64 equiv.
rol rbx,8 ; make room for last 2 bits coming
writeIt
ret
;;; code keeps on running onto PrintLine to finish off
;-------------------------------------------------------------------------
; IN: Nothing
; RETURNS: The Original text
; MODIFIES: Nothing
; CALLS: Kernel sys_write
PrintLine:
push rax ; Save all used registers
push rbx ; Save all used registers
push rcx ; Save all used registers
push rdx ; Save all used registers
mov rax,4 ; Specify sys_write call
mov rbx,1 ; Specify File Descriptor 1: Standard output
mov rcx,B64LIN ; Pass offset of line string
mov rdx,B64LEN ; Pass size of the line string
int 80h ; Make kernel call to display line string
pop rdx ; Restore all caller's registers
pop rcx ; dito
pop rbx ; dito
pop rax ; dito
ret ; Return to caller
;-------------------------------------------------------------------------
LoadBuff:
push rax ; Save caller's EAX
push rbx ; Save caller's EBX
push rdx ; Save caller's EDX
mov rax,3 ; Specify sys_read call
mov rbx,0 ; Specify File Descriptor 0: Standard Input
mov rcx,Buff ; Pass offset of the buffer to read to
mov rdx,BUFFLEN ; Pass number of bytes to read at one pass
int 80h ; Call sys_read to fill the buffer
mov rbp, rax ; Save # of bytes read from file for later
xor rcx,rcx ; Clear buffer pointer ECX to 0
pop rdx ; Restore caller's EDX
pop rbx ; Restore caller's EBX
pop rax ; Restore caller's EAX
ret ; And return to caller
GLOBAL _start
; ------------------------------------------------------------------------
; MAIN PROGRAM BEGINS HERE
;-------------------------------------------------------------------------
_start:
; We will stay into this loop until the buffer is empty
Read:
call LoadBuff ; Read first buffer of data from stdin
cmp rbp,0 ; If ebp=0, sys_read reached EOF on stdin
jbe Exit ; If ebp=0, we jumps to Exit
call Decode ; If there's still some data into the buffer, we call Encode to convert them
call PrintLine ; Save the enconded data into stdout
clear rbp ; Clear ebp for the next LoadBuff
jmp Read ; Read one more time the data from stdi
; The programm did his job, we can exit
Exit:
xor rax, rax ; Clear rax
xor rbx, rbx ; Clear rbx
mov rax,1 ; Code for Exit Syscall
mov rbx,0 ; Return a code of zero
int 0x80 ; Make kernel call`
[1]: https://i.stack.imgur.com/bHJnI.jpg

segmentation fault with assembler code [duplicate]

I am doing a proj. in 64-bit NASM. I have to convert decimal to binary and binary to decimal.
I keep getting segmentation fault after debugging when i call printf.
extern printf
section .bss
decsave: resd 2 ; stores dec->bin conversion
binsave: resd 1
section .data ; preset constants, writeable
dec1: db '1','2','4','.','3','7','5',0
bin1: dq 01010110110101B ; 10101101.10101 note where binary point should be
ten: dq 10
debug: db "debug 124 is %ld", 10, 0
section .text ; instructions, code segment
global main ; for gcc standard linking
main: ; label
push rbp ; save rbp
;parse and convert integer portion of dec->bin
mov rax,0 ; accumulate value here
mov al,[dec1] ; get first ASCII digit
sub al,48 ; convert ASCII digit to binary
mov rbx,0 ; clear register (upper part)
mov bl,[dec1+1] ; get next ASCII digit
sub rbx,48 ; convert ASCII digit to binary
imul rax,10 ; ignore rdx
add rax,rbx ; increment accumulator
mov rbx,0
mov bl,[dec1+2]
sub rbx,48
imul rax,10
add rax,rbx
mov [decsave],rax ; save decimal portion
mov rdi, debug
mov rsi, [decsave]
mov rax,0
call printf
; return using c-style pops to return stack to correct position
; and registers to correct content
pop rbp
mov rax,0
ret ; return
; print the bits in decsave:
section .bss
abits: resb 17 ; 16 characters & zero terminator
section .data
fmts: db "%s",0
section .text
; shift decimal portion into abits as ascii
mov rax,[decsave] ; restore rax to dec. portion
mov rcx,8 ; for printing 1st 8 bits
loop3: mov rdx,0 ; clear rdx ready for a bit
shld rdx,rax,1 ; top bit of rax into rdx
add rdx,48 ; make it ASCII
mov [abits+rcx-1],dl ; store character
ror rax,1 ; next bit into top of rax
loop loop3 ; decrement rcx, jump non zero
mov byte [abits+7],'.' ; end of dec. portion string
mov byte [abits+8],0 ; end of "C" string
push qword abits ; string to print
push qword fmts ; "%s"
call printf
add rsp,8
mov rax,[decsave+16] ; increment to fractional portion
mov rcx,16 ; for printing 3 bits as required in the directions
loop4: mov rdx,0 ; clear rdx ready for a bit
shld rdx,rax,1 ; top bit of rax into rdx
add rdx,48 ; make it ASCII
mov [abits+rcx-1],dl ; store character
ror rax,1 ; next bit into top of rax
loop loop4 ; decrement rcx, jump non zero
mov byte [abits+3],10 ; end of "C" string at 3 places
mov byte [abits+4],0 ; end of "C" string
push qword abits ; string to print
push qword fmts ; "%s"
call printf
add rsp,8
Is there a any other way to get around it?
Thank you.
As Jester pointed out, if the vararg function is not using sse, then al must be zero. There is a bigger issue here:
With the x86-64 calling convention, parameters are not passed on the stack as they are for 32bit, but instead passed through registers. Which registers all depend on what OS your program is written for.
x86 calling conventions

Manipulate string in assembly x86 (mov and print to screen)

I'm working on a larger project but I'm stuck with string manipulation. My assembly file includes math coprocessor operations (it starts the coprocessor with "FINIT") but I don't think it should be interfering at all.
Basically, I have some strings that are 50 bytes long each:
$s db 50 dup (?), '$'
_cte_14 db "hello world", '$', 39 dup (?)
I need to assign the value stored in variable "_cte_14" to variable "$s"
I attempted to use a register to temporarily store the value, like this:
mov cx, _cte_14
mov $s, cx
but I get the "operand types do not match" error.
Since I know the AX, BX, CX, DX registers only hold 16 bits, I thought maybe I need to work with the memory address of the first string character, so I tried:
mov bx, offset _cte_14
mov $s, bx
but the same error shows up.
I'm using TASM to compile for an x86 processor. What would be the right way to accomplish this?
Thanks a lot in advance.
Example for to copy the characters in a loop:
s db 51 dup ('$')
_cte_14 db "hello world"
len = ($ - _cte_14) ; (current location - offset _cte_14)
40 dup ('$')
mov si, offset _cte_14 ; get source offset
mov di, offset s ; get destination offset
mov cl, len ; length of the string
P1:
mov al, [si] ; get byte from DS:SI
mov [di], al ; store byte to DS:DI
dec cl ; decrease counter, set zero flag if zero
jnz P1 ; jump if zero flag is not set
-- Variation with using a string instruction together with a repeat instruction prefix:
mov si, offset _cte_14
mov di, offset s
mov cx, len ; length of the string
cld ; clear direction flag
rep movsb ; copy from DS:SI to ES:DI, increase SI+DI, decrease CX, repeat CX times

How do I convert a string representing a signed hex int into its signed int Doubleword number in 80x86?

So I am taking an assembly language course and I am stuck on this problem from the book:
Using the windows 32 console (so I have an io.h to use), I am supposed to take a valid hex value inputted by the user and then display the actual hex value in the register EAX. So if the user entered "AB CD E2 18", then after the procedure EAX would hold the value: ABCDE218.
The parts that I am stuck on are the A-F values. If I use A for example, I can get the bits to read 00000010, but I don't know how to change that into its hex value A. Here is what I have so far:
.586
.MODEL FLAT
.CODE
hexToInt PROC
push ebp ; save base pointer
mov ebp, esp ; establish stack frame
sub esp, 4 ; local space for sign
push ebx ; Save registers
push edx
push esi
pushfd ; save flags
mov esi,[ebp+8] ; get parameter (source addr)
WhileBlankD:
cmp BYTE PTR [esi],' ' ; space?
jne EndWhileBlankD ; exit if not
inc esi ; increment character pointer
jmp WhileBlankD ; and try again
EndWhileBlankD:
mov eax,1 ; default sign multiplier
IfPlusD:cmp BYTE PTR [esi],'+' ; leading + ?
je SkipSignD ; if so, skip over
IfMinusD:
cmp BYTE PTR [esi],'-' ; leading - ?
jne EndIfSignD ; if not, save default +
mov eax,-1 ; -1 for minus sign
SkipSignD:
inc esi ; move past sign
EndIfSignD:
mov [ebp-4],eax ; save sign multiplier
mov eax,0 ; number being accumulated
WhileDigitD:
cmp BYTE PTR [esi],'0' ; compare next character to '0'
jb EndWhileDigitD ; not a digit if smaller than '0'
cmp BYTE PTR [esi],'9' ; compare to '9'
ja TestForHexD
mov bl,[esi] ; ASCII character to BL
and ebx,0000000Fh ; convert to single-digit integer
and eax, ebx
shl eax, 4
inc esi
jmp WhileDigitD
TestForHexD:
cmp BYTE PTR [esi], 'F'
ja EndWhileDigitD
mov bl, [esi]
sub bl, 31h
and ebx, 000000FFh
or al, bl
shl eax, 4
inc esi ; increment character pointer
jmp WhileDigitD ; go try next character
EndWhileDigitD:
; if value is < 80000000h, multiply by sign
cmp eax,80000000h ; 80000000h?
jnb endIfMaxD ; skip if not
imul DWORD PTR [ebp-4] ; make signed number
endIfMaxD:
popfd ; restore flags
pop esi ; restore registers
pop edx
pop ebx
mov esp, ebp ; delete local variable space
pop ebp
ret ; exit
hexToInt ENDP
END
The TestForHex label is where I am trying to convert the ASCII string to hex. I was looking around and read that I could accomplish my goal by shifting and masking, but I can't figure it out and I can't find any examples. At this point I am sure its something really small that I am just over looking, but I am stuck.
There are some bugs in your code.
First, in 0 ... 9 string to integer conversion code, you don't do ASCII to binary conversion as you should do, but instead you do and ebx,0Fh, which is incorrect. You need to subtract '0' (30h) from each ASCII character, like this:
mov bl,[esi]
sub bl,'0' ; sub bl,30h
Then, also in 0 ... 9 string to integer conversion code:
and eax, ebx
If the number consists of only 0...9 digits, and eax, ebx will produce always 0. It should be:
or al,bl
Then, you do shl eax,4, even if you don't know if there will be more digits. That means that the number will be 16 times bigger than it should.
Then, you give the example input with spaces, but your code does not handle spaces (20h) properly, it ends reading input for any value below '0' (30h), it seems to accept only leading spaces (skip this if you don't want to accept spaces in between).
So, the entire code block above should be:
WhileDigitD:
cmp byte ptr [esi], ' ' ; delete this if you don't want spaces in between.
je next_char ; delete this if you don't want spaces in between.
cmp BYTE PTR [esi],'0' ; compare next character to '0'
jb EndWhileDigitD ; not a digit if smaller than '0'
cmp BYTE PTR [esi],'9' ; compare to '9'
ja TestForHexD
mov bl,[esi] ; ASCII character to BL
sub bl,'0' ; sub bl,30h -> convert ASCII to binary.
shift_eax_by_4_and_add_bl:
shl eax,4 ; shift the current value 4 bits to left.
or al,bl ; add the value of the current digit.
next_char:
inc esi
jmp WhileDigitD
I also added labels next_char and shift_eax_by_4_and_add_bl. The reason for next_char should be evident, shift_eax_by_4_and_add_bl is to minimize duplicate code of 0...9 and A...F code blocks, see below.
You don't check that that the hexadecimal A...F digit is within range A ... F, only that it's below or equal to F. Otherwise it has same bug with shl eax,4. And as usually duplicate code should be avoided, I added shift_eax_by_4_and_add_bl label to minimize duplicate code.
So I think it should be:
Edit: corrected sub bl,31h -> sub bl,('A'-0Ah).
TestForHexD:
cmp BYTE PTR [esi], 'A'
jb EndWhileDigitD
cmp BYTE PTR [esi], 'F'
ja EndWhileDigitD
mov bl,[esi]
sub bl,('A'-0Ah) ; sub bl,55 -> convert ASCII to binary.
jmp shift_eax_by_4_and_add_bl
If you need to convert a character (for simplicity, say, in upper case) representing a hex digit into the value of that digit you need to do this:
IF char >= 'A'
value = char - 'A' + 10
ELSE
value = char - '0'
ENDIF
If you need to do the reverse, you do the reverse:
IF value >= 10
char = value - 10 + 'A'
ELSE
char = value + '0'
ENDIF
Here you exploit the fact that the ASCII characters 0 through 9 have consecutive ASCII codes and so do the ASCII characters A through F.

How to print a number in assembly NASM?

Suppose that I have an integer number in a register, how can I print it? Can you show a simple example code?
I already know how to print a string such as "hello, world".
I'm developing on Linux.
If you're already on Linux, there's no need to do the conversion yourself. Just use printf instead:
;
; assemble and link with:
; nasm -f elf printf-test.asm && gcc -m32 -o printf-test printf-test.o
;
section .text
global main
extern printf
main:
mov eax, 0xDEADBEEF
push eax
push message
call printf
add esp, 8
ret
message db "Register = %08X", 10, 0
Note that printf uses the cdecl calling convention so we need to restore the stack pointer afterwards, i.e. add 4 bytes per parameter passed to the function.
You have to convert it in a string; if you're talking about hex numbers it's pretty easy. Any number can be represented this way:
0xa31f = 0xf * 16^0 + 0x1 * 16^1 + 3 * 16^2 + 0xa * 16^3
So when you have this number you have to split it like I've shown then convert every "section" to its ASCII equivalent.
Getting the four parts is easily done with some bit magic, in particular with a right shift to move the part we're interested in in the first four bits then AND the result with 0xf to isolate it from the rest. Here's what I mean (soppose we want to take the 3):
0xa31f -> shift right by 8 = 0x00a3 -> AND with 0xf = 0x0003
Now that we have a single number we have to convert it into its ASCII value. If the number is smaller or equal than 9 we can just add 0's ASCII value (0x30), if it's greater than 9 we have to use a's ASCII value (0x61).
Here it is, now we just have to code it:
mov si, ??? ; si points to the target buffer
mov ax, 0a31fh ; ax contains the number we want to convert
mov bx, ax ; store a copy in bx
xor dx, dx ; dx will contain the result
mov cx, 3 ; cx's our counter
convert_loop:
mov ax, bx ; load the number into ax
and ax, 0fh ; we want the first 4 bits
cmp ax, 9h ; check what we should add
ja greater_than_9
add ax, 30h ; 0x30 ('0')
jmp converted
greater_than_9:
add ax, 61h ; or 0x61 ('a')
converted:
xchg al, ah ; put a null terminator after it
mov [si], ax ; (will be overwritten unless this
inc si ; is the last one)
shr bx, 4 ; get the next part
dec cx ; one less to do
jnz convert_loop
sub di, 4 ; di still points to the target buffer
PS: I know this is 16 bit code but I still use the old TASM :P
PPS: this is Intel syntax, converting to AT&T syntax isn't difficult though, look here.
Linux x86-64 with printf
main.asm
default rel ; make [rel format] the default, you always want this.
extern printf, exit ; NASM requires declarations of external symbols, unlike GAS
section .rodata
format db "%#x", 10, 0 ; C 0-terminated string: "%#x\n"
section .text
global main
main:
sub rsp, 8 ; re-align the stack to 16 before calling another function
; Call printf.
mov esi, 0x12345678 ; "%x" takes a 32-bit unsigned int
lea rdi, [rel format]
xor eax, eax ; AL=0 no FP args in XMM regs
call printf
; Return from main.
xor eax, eax
add rsp, 8
ret
GitHub upstream.
Then:
nasm -f elf64 -o main.o main.asm
gcc -no-pie -o main.out main.o
./main.out
Output:
0x12345678
Notes:
sub rsp, 8: How to write assembly language hello world program for 64 bit Mac OS X using printf?
xor eax, eax: Why is %eax zeroed before a call to printf?
-no-pie: plain call printf doesn't work in a PIE executable (-pie), the linker only automatically generates a PLT stub for old-style executables. Your options are:
call printf wrt ..plt to call through the PLT like traditional call printf
call [rel printf wrt ..got] to not use a PLT at all, like gcc -fno-plt.
Like GAS syntax call *printf#GOTPCREL(%rip).
Either of these are fine in a non-PIE executable as well, and don't cause any inefficiency unless you're statically linking libc. In which case call printf can resolve to a call rel32 directly to libc, because the offset from your code to the libc function would be known at static linking time.
See also: Can't call C standard library function on 64-bit Linux from assembly (yasm) code
If you want hex without the C library: Printing Hexadecimal Digits with Assembly
Tested on Ubuntu 18.10, NASM 2.13.03.
It depends on the architecture/environment you are using.
For instance, if I want to display a number on linux, the ASM code will be different from the one I would use on windows.
Edit:
You can refer to THIS for an example of conversion.
I'm relatively new to assembly, and this obviously is not the best solution,
but it's working. The main function is _iprint, it first checks whether the
number in eax is negative, and prints a minus sign if so, than it proceeds
by printing the individual numbers by calling the function _dprint for
every digit. The idea is the following, if we have 512 than it is equal to: 512 = (5 * 10 + 1) * 10 + 2 = Q * 10 + R, so we can found the last digit of a number by dividing it by 10, and
getting the reminder R, but if we do it in a loop than digits will be in a
reverse order, so we use the stack for pushing them, and after that when
writing them to stdout they are popped out in right order.
; Build : nasm -f elf -o baz.o baz.asm
; ld -m elf_i386 -o baz baz.o
section .bss
c: resb 1 ; character buffer
section .data
section .text
; writes an ascii character from eax to stdout
_cprint:
pushad ; push registers
mov [c], eax ; store ascii value at c
mov eax, 0x04 ; sys_write
mov ebx, 1 ; stdout
mov ecx, c ; copy c to ecx
mov edx, 1 ; one character
int 0x80 ; syscall
popad ; pop registers
ret ; bye
; writes a digit stored in eax to stdout
_dprint:
pushad ; push registers
add eax, '0' ; get digit's ascii code
mov [c], eax ; store it at c
mov eax, 0x04 ; sys_write
mov ebx, 1 ; stdout
mov ecx, c ; pass the address of c to ecx
mov edx, 1 ; one character
int 0x80 ; syscall
popad ; pop registers
ret ; bye
; now lets try to write a function which will write an integer
; number stored in eax in decimal at stdout
_iprint:
pushad ; push registers
cmp eax, 0 ; check if eax is negative
jge Pos ; if not proceed in the usual manner
push eax ; store eax
mov eax, '-' ; print minus sign
call _cprint ; call character printing function
pop eax ; restore eax
neg eax ; make eax positive
Pos:
mov ebx, 10 ; base
mov ecx, 1 ; number of digits counter
Cycle1:
mov edx, 0 ; set edx to zero before dividing otherwise the
; program gives an error: SIGFPE arithmetic exception
div ebx ; divide eax with ebx now eax holds the
; quotent and edx the reminder
push edx ; digits we have to write are in reverse order
cmp eax, 0 ; exit loop condition
jz EndLoop1 ; we are done
inc ecx ; increment number of digits counter
jmp Cycle1 ; loop back
EndLoop1:
; write the integer digits by poping them out from the stack
Cycle2:
pop eax ; pop up the digits we have stored
call _dprint ; and print them to stdout
dec ecx ; decrement number of digits counter
jz EndLoop2 ; if it's zero we are done
jmp Cycle2 ; loop back
EndLoop2:
popad ; pop registers
ret ; bye
global _start
_start:
nop ; gdb break point
mov eax, -345 ;
call _iprint ;
mov eax, 0x01 ; sys_exit
mov ebx, 0 ; error code
int 0x80 ; край
Because you didn't say about number representation I wrote the following code for unsigned number with any base(of course not too big), so you could use it:
BITS 32
global _start
section .text
_start:
mov eax, 762002099 ; unsigned number to print
mov ebx, 36 ; base to represent the number, do not set it too big
call print
;exit
mov eax, 1
xor ebx, ebx
int 0x80
print:
mov ecx, esp
sub esp, 36 ; reserve space for the number string, for base-2 it takes 33 bytes with new line, aligned by 4 bytes it takes 36 bytes.
mov edi, 1
dec ecx
mov [ecx], byte 10
print_loop:
xor edx, edx
div ebx
cmp dl, 9 ; if reminder>9 go to use_letter
jg use_letter
add dl, '0'
jmp after_use_letter
use_letter:
add dl, 'W' ; letters from 'a' to ... in ascii code
after_use_letter:
dec ecx
inc edi
mov [ecx],dl
test eax, eax
jnz print_loop
; system call to print, ecx is a pointer on the string
mov eax, 4 ; system call number (sys_write)
mov ebx, 1 ; file descriptor (stdout)
mov edx, edi ; length of the string
int 0x80
add esp, 36 ; release space for the number string
ret
It's not optimised for numbers with base of power of two and doesn't use printf from libc.
The function print outputs the number with a new line. The number string is formed on stack. Compile by nasm.
Output:
clockz
https://github.com/tigertv/stackoverflow-answers/tree/master/8194141-how-to-print-a-number-in-assembly-nasm

Resources