flat assembler
Message board for the users of flat assembler.

Index > Main > About cbw,cwde,cwd,cdq

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1847
Roman 25 Apr 2023, 10:20
Why need cbw,cwde,cwd,cdq ?
cbw — sign-extend byte in al to word in ax. Why do this ?

Show example task where is needed cbw,cwde,cwd,cdq.
Post 25 Apr 2023, 10:20
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1847
Roman 25 Apr 2023, 10:27
O !
Post 25 Apr 2023, 10:27
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1847
Roman 25 Apr 2023, 10:27
I found. For negative numbers.

Code:
xor eax,eax
mov al,-5 ;-5=255-5=251
cbw       ;in ax=-5 but 16bits 65535-5
    
Post 25 Apr 2023, 10:27
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1040
Location: Russia
macomics 25 Apr 2023, 11:17
Forgot cqo. rax => rdx:rax
Post 25 Apr 2023, 11:17
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 25 Apr 2023, 11:21
In twos complement:
0xFB == -5
0xFFFB == -5

In unsigned:
0xFB == 251
0xFFFB == 65531

What happens if you add 65531 + 65531 with 16-bit registers?
0xFFFB + 0xFFFB == 0xFFF6 --> -5 + -5 == -10

What happens when you add 65531 + 5 with 16-bit registers?
0xFFFB + 0x0005 == 0x0000 --> -5 + 5 == 0
Post 25 Apr 2023, 11:21
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1040
Location: Russia
macomics 25 Apr 2023, 11:27
In order not to ask such questions, it is enough to read about the additional format of numbers.

https://en.wikipedia.org/wiki/Two%27s_complement#Sign_extension
Post 25 Apr 2023, 11:27
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 25 Apr 2023, 11:33
I think a lot of confusion happens if someone implicitly extends values with zeros.

The following might not be true:
0xFB == 0x00FB
We need to justify whether extending to the left with zeros is correct, or whether extending with the top-most bit is correct. And it depends upon the context.
Post 25 Apr 2023, 11:33
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1847
Roman 25 Apr 2023, 11:35
macomics wrote:
Forgot cqo. rax => rdx:rax

Why out to rdx:rax ?
Post 25 Apr 2023, 11:35
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1847
Roman 25 Apr 2023, 11:38
Oh !
Code:
mov     al,-5 ;al=FB
movsx ax, al  ;the same work as cbw ! I checked in IDA Pro ax=FFFB

;more handful then cbw,cwde,cwd
movsx   edx,cl
movsx   dx,cl
movsx   edx,cx
    

Crazy Intel Laughing


Last edited by Roman on 25 Apr 2023, 12:09; edited 7 times in total
Post 25 Apr 2023, 11:38
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 25 Apr 2023, 11:38
Roman wrote:
macomics wrote:
Forgot cqo. rax => rdx:rax

Why out to rdx:rax ?
For div
Post 25 Apr 2023, 11:38
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1040
Location: Russia
macomics 25 Apr 2023, 12:13
revolution wrote:
Roman wrote:
macomics wrote:
Forgot cqo. rax => rdx:rax

Why out to rdx:rax ?
For div
For idiv
Post 25 Apr 2023, 12:13
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 27 Apr 2023, 14:30
Roman wrote:
Oh !
Code:
mov     al,-5 ;al=FB
movsx ax, al  ;the same work as cbw ! I checked in IDA Pro ax=FFFB

;more handful then cbw,cwde,cwd
movsx   edx,cl
movsx   dx,cl
movsx   edx,cx
    

Crazy Intel Laughing

It’s just important to note that movsx and movzx didn’t exist before 80386.
Post 27 Apr 2023, 14:30
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 732
Ali.Z 16 May 2023, 18:23
you can play with the snippet here, trying removing cdq and see what happen.

https://board.flatassembler.net/topic.php?t=22616

_________________
Asm For Wise Humans
Post 16 May 2023, 18:23
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.