ASCII text is typically represented enclosed in quotes\' \' or \" \" and is repr
ID: 3684756 • Letter: A
Question
ASCII text is typically represented enclosed in quotes' ' or " " and is represented by byte values:
'0', '1', ... , '9' have the values: 30h, 31h, ..., 39h
'A', 'B', ..., 'Z' have the values: 41h, 42h, ..., 5Ah
'a', 'b', ..., 'z' have the values: 61h, 62h, ..., 7Ah
Given X is an ASCII string variable "1234" representing a decimal number. X will actually be stored as an array of bytes 31h, 32h, 33h, 34h. To convert X from an array into an integer value, V = 1234, use the following algorithm:
If X is a string representing a number of any base, b, the algorithm is slightly modified to:
d = 30h if the values are '0'-'9' but will be different (?) if the values were 'A'-'F' or 'a'-'z'
Integer to ASCII
Given A = 1234 is a decimal integer value that you wish to convert to an ASCII byte string X "1234":
V is an integer that is converted from an ASCII string representing a hex number. If string is "1234h" then V = 4660.
Explanation / Answer
Answer:
ASCII To Byte MASM x86 assembly language code :
.Ltext0:
.globl hex_decode
hex_decode:
.LFB0:
.cfi_startproc
0000 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
0001 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
0004 48897DE8 movq %rdi, -24(%rbp)
0008 8975E4 movl %esi, -28(%rbp)
000b 488955D8 movq %rdx, -40(%rbp)
000f C745F400 movl $0, -12(%rbp)
0016 C745F000 movl $0, -16(%rbp)
001d E9AC0000 jmp .L2
.L7:
0022 8B55F0 movl -16(%rbp), %edx
0025 488B45E8 movq -24(%rbp), %rax
0029 4801D0 addq %rdx, %rax
002c 0FB600 movzbl (%rax), %eax
002f 3C39 cmpb $57, %al
0031 7E15 jle .L3
0033 8B55F0 movl -16(%rbp), %edx
0036 488B45E8 movq -24(%rbp), %rax
003a 4801D0 addq %rdx, %rax
003d 0FB600 movzbl (%rax), %eax
0040 0FBEC0 movsbl %al, %eax
0043 83E837 subl $55, %eax
0046 EB13 jmp .L4
.L3:
0048 8B55F0 movl -16(%rbp), %edx
004b 488B45E8 movq -24(%rbp), %rax
004f 4801D0 addq %rdx, %rax
0052 0FB600 movzbl (%rax), %eax
0055 0FBEC0 movsbl %al, %eax
0058 83E830 subl $48, %eax
.L4:
005b 8945F8 movl %eax, -8(%rbp)
005e 8B45F0 movl -16(%rbp), %eax
0061 83C001 addl $1, %eax
0064 89C2 movl %eax, %edx
0066 488B45E8 movq -24(%rbp), %rax
006a 4801D0 addq %rdx, %rax
006d 0FB600 movzbl (%rax), %eax
0070 3C39 cmpb $57, %al
0072 7E1A jle .L5
0074 8B45F0 movl -16(%rbp), %eax
0077 83C001 addl $1, %eax
007a 89C2 movl %eax, %edx
007c 488B45E8 movq -24(%rbp), %rax
0080 4801D0 addq %rdx, %rax
0083 0FB600 movzbl (%rax), %eax
0086 0FBEC0 movsbl %al, %eax
0089 83E837 subl $55, %eax
008c EB18 jmp .L6
.L5:
008e 8B45F0 movl -16(%rbp), %eax
0091 83C001 addl $1, %eax
0094 89C2 movl %eax, %edx
0096 488B45E8 movq -24(%rbp), %rax
009a 4801D0 addq %rdx, %rax
009d 0FB600 movzbl (%rax), %eax
00a0 0FBEC0 movsbl %al, %eax
00a3 83E830 subl $48, %eax
.L6:
00c6 8345F002 addl $2, -16(%rbp)
00ca 8345F401 addl $1, -12(%rbp)
00a6 8945FC movl %eax, -4(%rbp)
00a9 8B45F4 movl -12(%rbp), %eax
00ac 488D1485 leaq 0(,%rax,4), %rdx
00b4 488B45D8 movq -40(%rbp), %rax
00b8 4801C2 addq %rax, %rdx
00bb 8B45F8 movl -8(%rbp), %eax
00be C1E004 sall $4, %eax
00c1 0B45FC orl -4(%rbp), %eax
00c4 8902 movl %eax, (%rdx)
.L2:
00ce 8B45E4 movl -28(%rbp), %eax
00d1 3B45F0 cmpl -16(%rbp), %eax
00d4 0F8748FF ja .L7
00da 488B45D8 movq -40(%rbp), %rax
00de 5D popq %rbp
.cfi_def_cfa 7, 8
00df C3 ret
.cfi_endproc
.LFE0:
.Letext0:
Integer To ASCII MASM x86 assembly language code :
.Ltext0:
.globl data
.data
.align 32
data:
0000 20 .byte 32
0001 00000000 .zero 999
.globl data1
03e8 00000000 .align 32
data1:
0400 20 .byte 32
0401 00000000 .zero 999
.comm val,4,4
.comm a,4,4
.comm varray,9,1
.text
.globl binary
binary:
.LFB0:
.cfi_startproc
0000 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
0001 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
0004 4883EC10 subq $16, %rsp
0008 897DFC movl %edi, -4(%rbp)
000b 837DFC00 cmpl $0, -4(%rbp)
000f 750A jne .L2
0011 C7050000 movl $48, val(%rip)
.L2:
001b 837DFC01 cmpl $1, -4(%rbp)
001f 750A jne .L3
0021 C7050000 movl $49, val(%rip)
.L3:
002b 837DFC02 cmpl $2, -4(%rbp)
002f 750A jne .L4
0031 C7050000 movl $50, val(%rip)
.L4:
003b 837DFC03 cmpl $3, -4(%rbp)
003f 750A jne .L5
0041 C7050000 movl $51, val(%rip)
.L5:
004b 837DFC04 cmpl $4, -4(%rbp)
004f 750A jne .L6
0051 C7050000 movl $52, val(%rip)
.L6:
005b 837DFC05 cmpl $5, -4(%rbp)
005f 750A jne .L7
0061 C7050000 movl $53, val(%rip)
.L7:
006b 837DFC06 cmpl $6, -4(%rbp)
006f 750A jne .L8
0071 C7050000 movl $54, val(%rip)
.L8:
007b 837DFC07 cmpl $7, -4(%rbp)
007f 750A jne .L9
0081 C7050000 movl $55, val(%rip)
.L9:
008b 837DFC08 cmpl $8, -4(%rbp)
008f 750A jne .L10
0091 C7050000 movl $56, val(%rip)
.L10:
009b 837DFC09 cmpl $9, -4(%rbp)
009f 750A jne .L11
00a1 C7050000 movl $57, val(%rip)
.L11:
00ab C7050000 movl $0, a(%rip)
00b5 EB51 jmp .L12
.L15:
00b7 8B050000 movl val(%rip), %eax
00bd 83E001 andl $1, %eax
00c0 85C0 testl %eax, %eax
00c2 7511 jne .L13
00c4 8B050000 movl a(%rip), %eax
00ca 4898 cltq
00cc C6800000 movb $48, varray(%rax)
00d3 EB0F jmp .L14
.L13:
00d5 8B050000 movl a(%rip), %eax
00db 4898 cltq
00dd C6800000 movb $49, varray(%rax)
.L14:
00e4 8B050000 movl val(%rip), %eax
00ea 89C2 movl %eax, %edx
00ec C1EA1F shrl $31, %edx
00ef 01D0 addl %edx, %eax
00f1 D1F8 sarl %eax
00f3 89050000 movl %eax, val(%rip)
00f9 8B050000 movl a(%rip), %eax
00ff 83C001 addl $1, %eax
0102 89050000 movl %eax, a(%rip)
.L12:
0108 8B050000 movl val(%rip), %eax
010e 85C0 testl %eax, %eax
0110 75A5 jne .L15
0112 EB1E jmp .L16
.L17:
0114 8B050000 movl a(%rip), %eax
011a 4898 cltq
011c C6800000 movb $48, varray(%rax)
0123 8B050000 movl a(%rip), %eax
0129 83C001 addl $1, %eax
012c 89050000 movl %eax, a(%rip)
.L16:
0132 8B050000 movl a(%rip), %eax
0138 83F807 cmpl $7, %eax
013b 75D7 jne .L17
013d C6050000 movb $0, varray+8(%rip)
0144 BF000000 movl $varray, %edi
0149 B8000000 movl $0, %eax
014e E8000000 call strrev
0153 BE000000 movl $varray, %esi
0158 BF000000 movl $data1, %edi
015d E8000000 call strcpy
0162 BE000000 movl $data, %esi
0167 BF000000 movl $data1, %edi
016c E8000000 call strcat
0171 BE000000 movl $data1, %esi
0176 BF000000 movl $data, %edi
017b E8000000 call strcpy
0180 C9 leave
.cfi_def_cfa 7, 8
0181 C3 ret
.cfi_endproc
.LFE0:
.section .rodata
.LC0:
0000 656E7465 .string "enter number"
.LC1:
000d 256400 .string "%d"
.text
.globl main
main:
.LFB1:
.cfi_startproc
0182 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
0183 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
0186 4883EC10 subq $16, %rsp
018a BF000000 movl $.LC0, %edi
018f E8000000 call puts
0194 488D45FC leaq -4(%rbp), %rax
0198 4889C6 movq %rax, %rsi
019b BF000000 movl $.LC1, %edi
01a0 B8000000 movl $0, %eax
01a5 E8000000 call __isoc99_scanf
01aa 8B45FC movl -4(%rbp), %eax
01ad 85C0 testl %eax, %eax
01af 750C jne .L19
01b1 BF000000 movl $0, %edi
01b6 E8000000 call binary
01bb EB4E jmp .L20
.L19:
01bd EB45 jmp .L21
.L22:
01bf 8B4DFC movl -4(%rbp), %ecx
01c2 BA676666 movl $1717986919, %edx
01c7 89C8 movl %ecx, %eax
01c9 F7EA imull %edx
01cb C1FA02 sarl $2, %edx
01ce 89C8 movl %ecx, %eax
01d0 C1F81F sarl $31, %eax
01d3 29C2 subl %eax, %edx
01d5 89D0 movl %edx, %eax
01d7 C1E002 sall $2, %eax
01da 01D0 addl %edx, %eax
01dc 01C0 addl %eax, %eax
01de 29C1 subl %eax, %ecx
01e0 89CA movl %ecx, %edx
01e2 89D7 movl %edx, %edi
01e4 E8000000 call binary
01e9 8B4DFC movl -4(%rbp), %ecx
01ec BA676666 movl $1717986919, %edx
01f1 89C8 movl %ecx, %eax
01f3 F7EA imull %edx
01f5 C1FA02 sarl $2, %edx
01f8 89C8 movl %ecx, %eax
01fa C1F81F sarl $31, %eax
01fd 29C2 subl %eax, %edx
01ff 89D0 movl %edx, %eax
0201 8945FC movl %eax, -4(%rbp)
.L21:
0204 8B45FC movl -4(%rbp), %eax
0207 85C0 testl %eax, %eax
0209 7FB4 jg .L22
.L20:
020b BF000000 movl $data, %edi
0210 E8000000 call puts
0215 C9 leave
.cfi_def_cfa 7, 8
0216 C3 ret
.cfi_endproc
.LFE1:
.Letext0: